Before writing the web did not apply to multi-threading and queue knowledge, wrote a small program, practiced number one, simulated merchandise snapped, the code is as follows:
classProgram { Public StaticRandom r =NewRandom (); Public Static intGoodsstock = -; Public StaticQueue Payq =NewQueue (); Public StaticQueue WAITQ =NewQueue (); Static voidMain (string[] args) { //increase the monitoring threadThreadStart CHILDREF3 =NewThreadStart (Lookatactivethread); Console.WriteLine ("the monitoring thread is up! "); Thread childThread3=NewThread (CHILDREF3); Childthread3.start (); //increase user snapping threadThreadStart CHILDREF1 =NewThreadStart (Userbuyinsertthread); Console.WriteLine ("user base 1 started snapping up! "); Thread ChildThread1=NewThread (CHILDREF1); Childthread1.start (); //increase user snapping threadThreadStart CHILDREF4 =NewThreadStart (Userbuyinsertthread); Console.WriteLine ("user base 2 started snapping up! "); Thread ChildThread4=NewThread (CHILDREF4); Childthread4.start (); //Increase start Checkout threadThreadStart Childref2 =NewThreadStart (Userpayendthread); Console.WriteLine ("Checkout thread has started! "); Thread childThread2=NewThread (CHILDREF2); Childthread2.start (); Console.readkey (); } //monitor thread, close all threads after snapping ends Public Static voidLookatactivethread () {DateTime TEMPDT=NewDateTime (); intTotalCount =Goodsstock; while(true) { if(Goodsstock = =0&& Payq. Count = =0&& WAITQ. Count = =0) {TimeSpan TS=DateTime.Now.Subtract (TEMPDT); Console.WriteLine ("The snapping campaign is over,"+ TotalCount +"a product has been"+ TS. Seconds +"seconds sold out current time:"+DateTime.Now); Break; } Else{Thread.Sleep ( -); } } } //purchase user randomly queued Insert queue Total number of people cannot exceed 10 Public Static voidUserbuyinsertthread () {Try { while(true) { if(Goodsstock >0) { //determines whether the wait queue is empty, whether the payment queue is full, and joins directly into the payment queue if(WAITQ. count==0&& Payq. Count <Goodsstock) { intUserID = R.next (10000,99999); Payq. Enqueue (userid); Console.WriteLine ("User"+ userid +"to order successfully, please pay the current payment queue as soon as possible:"+ Payq. Count +"Current Time:"+DateTime.Now); Thread.Sleep (R.next ( -)); } Else { intUserID = R.next (10000,99999); WAITQ. Enqueue (userid); Console.WriteLine ("User"+ userid +"The current waiting queue is joined to the wait queue:"+ WAITQ. Count +"Current Time:"+DateTime.Now); Thread.Sleep (R.next ( -, -)); } } Else{Console.WriteLine ("Sorry, you are late, the product is sold out"); Thread.Sleep ( -); } if(Goodsstock = =0&& Payq. Count = =0&& WAITQ. Count = =0) { //stopping a threadConsole.WriteLine ("Queue thread End current time:"+DateTime.Now); Break; } } } Catch(ThreadAbortException e) {Console.WriteLine ("the user queued thread is faulted:"+e.tostring ()); } } //User purchase operation, payment time is relatively random Public Static voidUserpayendthread () {Try { while(true) { if(Payq. Count = =0&& WAITQ. Count = =0) {Thread.Sleep ( -); } Else { if(Goodsstock = =0) { if(WAITQ. Count >0) { intUserID = (int) Waitq. Dequeue (); Console.WriteLine ("Sorry"+ userid +", the goods have been sold out! Please try harder next time! Current time:"+DateTime.Now); Thread.Sleep ( -); } } Else { if(Payq. Count >0) { intUserID = (int) Payq. Dequeue (); //a certain probability of the user timeout or cancel the order if(R.next (1,Ten) <3) {Console.WriteLine ("Sorry, user"+ userid +"has timed out or canceled orders! Current Payment queue:"+ Payq. Count +"Current Time:"+ DateTime.Now +"Current Inventory:"+Goodsstock); } Else{Goodsstock-=1; Console.WriteLine ("Congratulations! User"+ userid +"already checkout, purchase success! Current Payment queue:"+ Payq. Count +"Current Time:"+ DateTime.Now +"Current Inventory:"+Goodsstock); } //add users in the wait queue to the payment queue to determine whether the payment queue exceeds the inventory quantity s if(Goodsstock >0&& goodsstock > Payq. Count && WAITQ. Count >0) {userid= (int) Waitq. Dequeue (); Payq. Enqueue (userid); Console.WriteLine ("User"+ userid +"you have joined to the payment queue from the queued queue, please pay as soon as possible! Current Payment queue:"+ Payq. Count +"Current Time:"+ DateTime.Now +"Current Inventory:"+Goodsstock); } thread.sleep (R.next ( -, -)); } } } if(Goodsstock = =0&& Payq. Count = =0&& WAITQ. Count = =0) { //stopping a threadConsole.WriteLine ("Payment thread End Current time:"+DateTime.Now); Break; } } } Catch(ThreadAbortException e) {Console.WriteLine ("There was an error in the user checkout thread:"+e.tostring ()); } } }
View Code
C # Multi-threading and queue operation Small drill knife