Applying the CLR's thread pool

Source: Internet
Author: User

As you all know, the creation and destruction of this thread requires a lot of performance overhead, and when there are more and different tasks that need to be done, consider using a thread pool to manage those threads. Each process on a Windows NT-based operating system contains a thread pool, where there are many threads that can be used at any time, and when the application is in use, a thread that already exists is removed from the thread pool and is not destroyed immediately after the use of a thread is complete. Instead, it is placed in the thread pool to wait for the next use.

In C #, through system. Threading.threadpool provides the following common methods and instructions.

Method name Description
Bindhandle Binds an operating system handle to ThreadPool.
GetAvailableThreads Retrieves the difference between the maximum number of threads returned by the GetMaxThreads method and the number of currently active threads.
GetMaxThreads Retrieves the number of thread pool requests that are active at the same time, and all requests that are greater than this number remain queued until the thread pool threads become available.
GetMinThreads Retrieves the number of idle threads that the thread pool maintains in the new request forecast.
QueueUserWorkItem The method is queued for execution, and this method executes when the thread pool threads become available.
RegisterWaitForSingleObject Registers the delegate that is waiting for WaitHandle.
SetMaxThreads Sets the number of requests for a thread pool that can be active at the same time. All requests that are greater than this number remain queued until the thread pool threads become available.
SetMinThreads Sets the number of idle threads that the thread pool maintains in the new request forecast.
Unsafequeueuserworkitem Register a delegate waiting for WaitHandle.
Unregisterwaitforsingleobject Queues the specified delegate to the thread pool.

As an example:

 Static voidMain (string[] args) {            intintworkthreads; intintcompletionportthreads; Threadpool.getmaxthreads ( outIntworkthreads, outintcompletionportthreads); Console.WriteLine ("maximum number of worker threads: {0}, max I/O threads: {1}", intworkthreads,intcompletionportthreads); //gets information about the maximum number of threads.              for(inti =0; I <3; i++) {ThreadPool.QueueUserWorkItem (testthreadpool);//request threads in the thread pool. } console.readline (); }        Static voidTestthreadpool (Objectobj) {             for(inti =0; I <3; i++)            {                //Thread.CurrentThread.ManagedThreadId Gets the identity of the running thread. Console.WriteLine ("thread pool, the unique identity of the running thread is {0}", Thread.CurrentThread.ManagedThreadId); } thread.sleep ( -); }

Threads running in the thread pool are background threads, that is, the IsBackground property of all threads in the thread pool is set to true, the background thread does not affect the end of the application, and the foreground thread has a significant effect on the end of the application. Because the application exits must wait for all foreground threads to end.

Applying the CLR's thread pool

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.