Thread Learning-1, controlling the number of threads, imitating the

Source: Internet
Author: User

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

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.