1. 程式人生 > >C# 多線程與隊列操作小練刀

C# 多線程與隊列操作小練刀

art nds b- ext next else and 一把手 timespan

之前寫Web沒有應用到多線程與隊列的知識,寫了個小程序,練了一把手,模擬商品搶購,代碼如下:

技術分享
 class Program
    {
        public static Random r = new Random();
        public static int GoodsStock = 30;
        public static Queue payq = new Queue();
        public static Queue waitq = new Queue();

        static void Main(string[] args)
        {
            
//增加監視線程 ThreadStart childref3 = new ThreadStart(LookAtActiveThread); Console.WriteLine("監視線程啟動了!"); Thread childThread3 = new Thread(childref3); childThread3.Start(); //增加用戶搶購線程 ThreadStart childref1 = new ThreadStart(UserBuyInsertThread); Console.WriteLine(
"用戶群1開始搶購了!"); Thread childThread1 = new Thread(childref1); childThread1.Start(); //增加用戶搶購線程 ThreadStart childref4 = new ThreadStart(UserBuyInsertThread); Console.WriteLine("用戶群2開始搶購了!"); Thread childThread4 = new Thread(childref4); childThread4.Start();
//增加開始結賬線程 ThreadStart childref2 = new ThreadStart(UserPayEndThread); Console.WriteLine("結賬線程啟動了!"); Thread childThread2 = new Thread(childref2); childThread2.Start(); Console.ReadKey(); } //監視線程,搶購結束後關閉所有線程 public static void LookAtActiveThread() { DateTime TempDT = new DateTime(); int totalCount = GoodsStock; while (true) { if (GoodsStock == 0 && payq.Count == 0 && waitq.Count == 0) { TimeSpan ts = DateTime.Now.Subtract(TempDT); Console.WriteLine("搶購活動已結束," + totalCount + "個商品已在" + ts.Seconds + "秒售罄 當前時間:" + DateTime.Now); break; } else { Thread.Sleep(100); } } } //購買用戶隨機排隊插入隊列 排隊總人數不能超過10個 public static void UserBuyInsertThread() { try { while (true) { if (GoodsStock > 0) { //判斷等待隊列是否為空,付款隊列是否已滿,直接加入到付款隊列中 if (waitq.Count==0 && payq.Count < GoodsStock) { int userid = r.Next(10000, 99999); payq.Enqueue(userid); Console.WriteLine("用戶" + userid + "下單成功,請盡快付款 當前付款隊列:" + payq.Count + " 當前時間:" + DateTime.Now); Thread.Sleep(r.Next(100)); } else { int userid = r.Next(10000, 99999); waitq.Enqueue(userid); Console.WriteLine("用戶" + userid + "已加入到等待隊列中 當前等待隊列:" + waitq.Count + " 當前時間:" + DateTime.Now); Thread.Sleep(r.Next(500, 2000)); } } else { Console.WriteLine("對不起,您來晚了,商品已售罄"); Thread.Sleep(300); } if (GoodsStock == 0 && payq.Count == 0 && waitq.Count == 0) { //停止線程 Console.WriteLine("排隊線程結束 當前時間:" + DateTime.Now); break; } } } catch (ThreadAbortException e) { Console.WriteLine("用戶排隊線程出錯了:" + e.ToString()); } } //用戶購買操作,付款時間比較隨機 public static void UserPayEndThread() { try { while (true) { if (payq.Count == 0 && waitq.Count == 0) { Thread.Sleep(300); } else { if (GoodsStock == 0) { if (waitq.Count > 0) { int userid = (int)waitq.Dequeue(); Console.WriteLine("對不起" + userid + ",商品已被搶購一空! 下次請努力! 當前時間:" + DateTime.Now); Thread.Sleep(100); } } else { if (payq.Count > 0) { int userid = (int)payq.Dequeue(); //用戶一定概率的超時或者取消訂單 if (r.Next(1, 10) < 3) { Console.WriteLine("對不起,用戶" + userid + "已超時或取消訂單! 當前付款隊列:" + payq.Count + " 當前時間:" + DateTime.Now + " 當前庫存:" + GoodsStock); } else { GoodsStock -= 1; Console.WriteLine("恭喜!用戶" + userid + "已結賬,購買成功! 當前付款隊列:" + payq.Count + " 當前時間:" + DateTime.Now + " 當前庫存:" + GoodsStock); } //把等待隊列中的用戶加入到付款隊列中,判斷付款隊列中是否超過庫存數量s if (GoodsStock > 0 && GoodsStock > payq.Count && waitq.Count > 0) { userid = (int)waitq.Dequeue(); payq.Enqueue(userid); Console.WriteLine("用戶" + userid + "已從排隊隊列加入到付款隊列,請盡快付款!當前付款隊列:" + payq.Count + " 當前時間:" + DateTime.Now + " 當前庫存:" + GoodsStock); } Thread.Sleep(r.Next(100, 500)); } } } if (GoodsStock == 0 && payq.Count == 0 && waitq.Count == 0) { //停止線程 Console.WriteLine("付款線程結束 當前時間:" + DateTime.Now); break; } } } catch (ThreadAbortException e) { Console.WriteLine("用戶結賬線程出錯了:" + e.ToString()); } } }
View Code

C# 多線程與隊列操作小練刀