C # thread pool details

Source: Internet
Author: User

Here you can learn how Microsoft studies the principle mechanism of CLR implementing the thread pool, so as to more flexibly handle the thread pool problem of CLR in actual code. Let's take a look at it.

Generation of the thread pool in the CLR tutorial

During CLR initialization, the thread pool does not contain threads. When an application needs to create a thread to execute the task, the application should request the thread pool thread to execute the task. An initial thread will be created after the thread pool is known. The initialization of the new thread is the same as that of other threads. However, after the task is completed, the thread will not be destroyed by itself. Instead, it returns the thread pool in the suspended state. If the application sends a request to the thread pool again, the suspended thread activates and executes the task without creating a new thread. This saves a lot of expenses. As long as the queuing speed of application tasks in the thread pool is lower than the speed at which one thread processes each task, the same thread can be reused repeatedly to save a lot of overhead during the lifetime of the application.

Then, if the application task queuing speed in the thread pool exceeds the speed at which one thread processes the task, an additional thread will be created in the thread pool. Of course, creating a new thread will produce additional overhead, but during its lifetime, the application may only request several threads to process all the tasks assigned to it. Therefore, in general, the performance of applications can be improved by using the thread pool. A wonderful feature of the thread pool is that it is heuristic. If your application needs to execute many tasks, more threads will be created in the thread pool. If the workload of your application is gradually reduced, the thread pool thread will terminate on its own. The thread pool algorithm ensures that it only contains the number of threads required for the workload on it!

Therefore, we hope that you have understood the basic concepts of the thread pool and the performance advantages it can provide. Now I will provide some code to illustrate how to use the thread pool. First, you should know that the Thread Pool provides four functions:
◆ Asynchronous call Method
◆ Call the method at a certain interval
◆ Call method when a single kernel object receives a signal notification
◆ Call method when the asynchronous I/O Request ends

The first three functions are very useful. I will explain them in this column. Application developers seldom use the fourth feature, so I will not describe it here; it is possible to talk about it in future columns.

Function 1: CLR thread pool tutorial-asynchronous call Method

In your application, if you have a new thread to execute the task code, I suggest you replace it with the new code for executing the task in the Command thread pool. In fact, you usually find that it is easier to run tasks in the thread pool than to run tasks in a new dedicated thread. To queue thread pool tasks, you can use the ThreadPool class defined in the System. Threading namespace. The ThreadPool class only provides static methods and cannot construct its instances. To allow the thread pool thread to call the method asynchronously, your code must call the overload QueueUserWorkItem method of a ThreadPool, as shown below:

Public static Boolean QueueUserWorkItem (WaitCallback wc, Object state); public static Boolean QueueUserWorkItem (WaitCallback wc); these methods set "work items" (and optional state data) queue in the thread pool and return immediately. A work item is only a method (identified by the wc parameter) that is called and passed to a single parameter, that is, status data ). The QueueUserWorkItem version without the status parameter passes null to the callback method. Finally, some threads in the pool will call your method to process the work item. The callback method you write must match the System. Threading. WaitCallback delegate type. Its definition is as follows:

Public delegate void WaitCallback (Object state );

Sample Code:

01 using System; 02 using System.Threading; 03   04 public class Test 05 { 06      // Store the field of the value to be calculated 07      static double number1 = -1; 08      static double number2 = -1; 09   10      public static void Main() 11      { 12          // Obtain the maximum number of threads in the thread pool and the minimum number of Idle threads maintained 13          int maxThreadNum, portThreadNum; 14          int minThreadNum; 15          ThreadPool.GetMaxThreads( out maxThreadNum, out portThreadNum); 16          ThreadPool.GetMinThreads( out minThreadNum, out portThreadNum); 17          Console.WriteLine( "Most

Related Article

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.