C # threads are executed sequentially.

Source: Internet
Author: User

Today saw Jetlian's blog, oneself also realized a bit, the general same, partially modified a bit. There are mistakes in the hope of the generous enlighten, thank you!

Http://www.cnblogs.com/jetlian/archive/2015/03/01/4307584.html

1 class Program2     {3         Static voidMain (string[] args)4         {5Threadqueue pool =NewThreadqueue (New string[] {"A","B","C","C","C"},1);6 Console.readkey ();7         }8     }9 Ten     classThreadqueue One     { A         Private Static Object_lockobj =New Object();//Lock Object -         Private Static string[] _reportlist;//the printed collection -         Private Static int_curindex =-1;//Current subscript the         Private Static int_reportcount;//Print Count -         Private Static int_curnumber;//Current Times -          PublicThreadqueue (string[] reportlist,intcount) -         { +             if(Reportlist = =NULL|| Reportlist.length <1|| Count <1) -             { +                 return; A             } at_reportcount =count; -_reportlist =reportlist; -              for(inti =0; i < reportlist.length; i++) -             { -                 NewThread (printnumber) {Name ="Thread"+ i}. Start (Newkeyvaluepair<int,string>(i, reportlist[i])); -             } in         } -  to         /// <summary> +         ///Implementing queued Printing -         /// </summary> the         /// <param name= "Objdata" ></param> *         Private voidPrintnumber (Objectobjdata) $         {Panax Notoginseng              while(_curnumber <_reportcount) -             { thekeyvaluepair<int,string> data = (keyvaluepair<int,string>) Objdata; +                 Lock(_lockobj) A                 { the                     if(Data. Key = = _curindex +1|| (_curindex = = _reportlist.length-1&& data. Key = =0)) +                     { -Console.WriteLine ("{0}:{1}", Thread.CurrentThread.Name, data. Value); $_curindex =data. Key; $                         if(_curindex = = _reportlist.length-1) -                         { -_curnumber++; the                         } - Monitor.pulseall (_lockobj);Wuyi                     } the                     Else -                     { Wu monitor.wait (_lockobj); -                     } About                 } $             } -         } -}

C # threads are executed sequentially.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.