Learned to control the number of threads, the current feeling that the use of c#5.0 new features flexibility than the previous thread use, so not very like to use the new features, but also may be I am not familiar with, and so familiar with the later, and then review good, today first engage in a control of the number of threads
Public classTasktest { Public voidTest () {List<Task> list =NewList<task>(); TaskFactory TaskFactory=NewTaskFactory (); for(inti =0; I < -; i++) {list. ADD (Taskfactory.startnew ( This. Clacnum)); if(list. Count>8) { //Re -Assign the unfinished, no-exception, non-canceled thread task to the listList = list. Where (x =!x.iscompleted &&!x.iscanceled &&!x.isfaulted). ToList (); //wait for the completion of one of the thread tasksTask.waitany (list. ToArray ()); } } } Private voidClacnum () {Longnum =0; for(inti =0; I < +; i++) {num+=i; Thread.Sleep (4); } Console.WriteLine ("Current thread: {0}, return value: {1}", Thread.CurrentThread.ManagedThreadId, num); } }
Thread resources can be reused and no new threads are available
Thread Learning-1, controlling the number of threads, imitating the