About the. net framework thread pool,. netframework

Source: Internet
Author: User

About the. net framework thread pool,. netframework

The thread pool is the basis for concurrent development of C #. Tasks in C # and asynchronous delegation are used internally. All threads in the thread pool are background threads.

As the name suggests, a thread pool has a certain number of active threads for program calls. In windows, a process is a collection of program resources, and a thread is a real execution unit. If a host has multiple logical CPUs, the program can run in parallel. However, it takes time and memory to create a thread. If a thread is created and destroyed frequently, the program performance will be greatly affected. The thread pool mechanism can make up for this. When an asynchronous task is executed using the thread pool, the task scheduler puts the task in the thread pool for execution. After the task is completed, it does not destroy the thread, but continues to wait for other tasks of the user to be executed. If all threads in the thread pool are executing tasks, the thread pool creates a new thread.

// Obtain the minimum number of threads int minWorkerThread, minPortThread, and ThreadPool. getMinThreads (out minWorkerThread, out minPortThread); Console. writeLine (minWorkerThread. toString () + "" + minPortThread. toString (); // get the maximum number of threads int maxWorkerThread, maxPortThread; ThreadPool. getMaxThreads (out maxWorkerThread, out maxPortThread); Console. writeLine (maxWorkerThread. toString () + "" + maxPortThread. toString (); // obtain the number of threads that can be created (that is, the difference between the maximum number of threads and the number of existing threads in the thread pool) int availableWorkerThread, availablePortThread; ThreadPool. getAvailableThreads (out availableWorkerThread, out availablePortThread); Console. writeLine (availableWorkerThread. toString () + "" + availablePortThread. toString ());

The thread pool has the minimum number of threads and the maximum number of threads. The minimum number of threads is generally the same as the number of logical CPUs. The maximum number of threads is 32767 (worker thread) and 1000 (IO thread) in. net4.0 ). You can set the maximum number of threads and the minimum number of threads. When the number of threads in the thread pool is lower than the minimum number of threads, a new thread is created immediately. When the number of threads is greater than the minimum number of threads, the thread pool will wait 500 milliseconds to check whether a new thread needs to be created. Therefore, the time required to create a new thread is greater than 500 milliseconds. When the maximum number of threads is reached, no new threads will be created. If a new task exists, the thread pool queues the task until there are available threads. In addition, when the thread pool thread is idle, it will be destroyed automatically after a certain period of time.

The thread pool is generally used to execute tasks that have a short execution time and do not require strict execution time and sequence. This maximizes the advantages of the thread pool. In addition, when the minimum number of threads is exceeded, the creation time of new threads will be greater than 500 milliseconds. You can modify the minimum number of threads in the thread pool as needed to meet the actual needs of the program.

// Set only the worker thread here // set int appMinThreadCount = 5 as needed; // obtain the minimum number of threads int minWorkerThread, minPortThread, and ThreadPool. getMinThreads (out minWorkerThread, out minPortThread); if (appMinThreadCount> minPortThread) {var result = ThreadPool. setMinThreads (appMinThreadCount, minPortThread); // the return value is used to determine whether the Console is set successfully. writeLine (result );}

 

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.