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.