Multi-threaded implementation Thread.Start () and ThreadPool.QueueUserWorkItem two ways of comparison

Source: Internet
Author: User

Thread.Start (), ThreadPool.QueueUserWorkItem is a common method in implementing multithreaded parallel programming. What are the similarities and differences between the two ways, and how to choose?

Write a demo that is implemented in two different ways. Observe the individual phenomena.

A workman class in which the method dosomething () is used for each asynchronous thread invocation. The method just randomly lets the thread hibernate for a while.

1  Public voiddosomething ()2 {3Onbegin (NewEventArgs ());4 5      //someone does something here6      varR =NewRandom ();7      intSleeptime = R.next ( the,180000);8Thread.Sleep (900000);9 TenOnCompleted (NewEventArgs ()); One}
dosomething

Thread.Start () mode implementation

Workthreads =NewThread[number_of_threads]; for(vari =0; i < number_of_threads; i++) {Arrworkmen[i]=NewWorkMan () {workstarted=true, InstanceID=Startthreadnumber}; Arrworkmen[i]. Beginhandler+=Handletaskbegin; Arrworkmen[i]. Completedhandler+=handletaskcompleted; //Create a thread and attach to the object    varSt =NewThreadStart (arrworkmen[i].dosomething); Workthreads[i]=NewThread (ST); Startthreadnumber++;} for(vari =0; i < number_of_threads; i++) {Thread.Sleep ( -); Workthreads[i].                Start ();} 
Thread.Start ()

ThreadPool.QueueUserWorkItem Method implementation

1  for(vari =0; i < number_of_threads; i++)2 {3Arrworkmen[i] =NewWorkMan ()4     {5workstarted =true,6InstanceID =Startthreadnumber7     };8 9Arrworkmen[i]. Beginhandler + =Handletaskbegin;TenArrworkmen[i]. Completedhandler + =handletaskcompleted; One  Astartthreadnumber++; - } -  the  for(vari =0; i < number_of_threads; i++) - { -Thread.Sleep ( -); -ThreadPool.QueueUserWorkItem (o =arrworkmen[i].dosomething ()); +}
ThreadPool.QueueUserWorkItem

Observe the case of thread creation and recycling in two ways.

In the same scenario, a new thread is launched every 2 seconds and sleeps for 2 minutes per thread. Thread.Start () Implementation, the thread up to 71, and then with 2 minutes after the end of the dormant thread, the number of threads always hovering between 70 and 71. The implementation of ThreadPool.QueueUserWorkItem, the number of threads reached 73, always hovering between 72 and 73.

In general, do the same thing. The ThreadPool method produces a slightly higher number of threads than Thread.Start (). Thread.Start () The resulting thread is quickly reclaimed by the system after the task is completed. In the ThreadPool (thread pool) mode, threads are retained for a period of time for Resue after they have completed their work. Therefore, it is not recommended to use the ThreadPool method when the requirements require a large number of threads to work concurrently because it keeps a lot of extra threads.

A reference from the network is excerpted here:

ThreadPoolAs for the, it's designed to use as few threads as possible while also keeping the CPU busy. Ideally, the number of busy threads is equal to the number of CPU cores. However, if the pool detects that its threads is currently not using the The CPU (sleeping, or waiting for another thread), I T starts up + threads (at a rate of 1/second, up to some maximum) to keep the CPU busy.

Demo Source: Multiplethreadswaydemo

Multi-threaded implementation Thread.Start () vs. ThreadPool.QueueUserWorkItem two different ways

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.