Also talk about. Net Thread Pool

Source: Internet
Author: User
Tags introductions net thread ranges

Recently, we have been busy with system optimization, but it also involves. net thread pool, about. net thread pool has many introductions, but some problems are found in the Process of use, which is inconsistent with the msdn and online Introduction:

I. Default thread pool settings

Let's take a look at Microsoft's official statement:

The default value of maxiothreads is 20. This attribute ranges from 5 to 100. The default value of maxworkerthreads is 20. This attribute ranges from 5 to 100. The default value of miniothreads is 1. The default value of minworkerthreads is 1.

The default value of requestqueuelimit is 5000.

 

Jeffer richer description

. NET 2.0 has 25 threads per CPU, and it is not recommended to modify the default configuration. Many people on the internet think it is 25, but I did not measure this result.

 

My tests:

My machine is dual-core, AutoConfig = "true"

Windows application:

Maximum number of threads: 500

Minimum

Maximum Io count: 1000

 

Web application:

Maximum threads: 200

Maximum Io count: 1000

Test method:

 

Int I = 0; Int J = 0; system. diagnostics. stopwatch watch = new stopwatch (); watch. start (); threadpool. getmaxthreads (Out I, out J); response. write ("Maximum working thread" + I. tostring () + "/R/N"); threadpool. getminthreads (Out I, out J); response. write ("minimum working thread" + I. tostring () + "/R/N"); threadpool. getavailablethreads (Out I, out J); response. write ("available threads:" + I. tostring () + "/R/N"); thread. sleep (5000); watch. stop (); decimal Tim = watch. elapsed. ticks/10 m; response. write ("time spent:" + Tim. tostring ());

 

 

Note: according to the test, the minimum number of threads is the same as that described on msdn. The default value is 1, but the maximum number of threads should be Windows applications (CPU x 250 ), web applications (CPU x 100), So If AutoConfig = "true" is not 20 or 25, but 100, the test results are consistent on different machines.

 

Ii. Significance of Optimization

According to the official statement, the creation of threads consumes a lot of system resources. Therefore, when the user traffic is very high, the number of minworkerthreads and other configurations should be increased, and AutoConfig = "false"

Modify to false and specify maxworkerthreads = "20" minworkerthreads = "10 ".CodeSleep the thread for 5 seconds and press 500 concurrent threads. In the second half of the test, we found that the number of available threads (maximum number of threads-number of used threads) reached about 9. When the concurrency is stopped, it takes a while for the number of available threads to be restored. In this case, we thought the setting range was large and re-set maxworkerthreads = "10" minworkerthredad = "5" and pressed it again, it is found that the number of available threads is maintained at around 7 and is not exhausted. Therefore, it is initially considered that the threadpool has its own internal protection mechanism, and the number of available threads must be reserved to deal with other processing.

 

Recently, we have been busy with system optimization, but it also involves. net thread pool, about. net thread pool has many introductions, but some problems are found in the Process of use, which is inconsistent with the msdn and online Introduction:

I. Default thread pool settings

Let's take a look at Microsoft's official statement:

The default value of maxiothreads is 20. This attribute ranges from 5 to 100. The default value of maxworkerthreads is 20. This attribute ranges from 5 to 100. The default value of miniothreads is 1. The default value of minworkerthreads is 1.

The default value of requestqueuelimit is 5000.

 

Jeffer richer description

. NET 2.0 has 25 threads per CPU, and it is not recommended to modify the default configuration. Many people on the internet think it is 25, but I did not measure this result.

 

My tests:

My machine is dual-core, AutoConfig = "true"

Windows application:

Maximum number of threads: 500

Minimum

Maximum Io count: 1000

 

Web application:

Maximum threads: 200

Maximum Io count: 1000

Test method:

 

Int I = 0; Int J = 0; system. diagnostics. stopwatch watch = new stopwatch (); watch. start (); threadpool. getmaxthreads (Out I, out J); response. write ("Maximum working thread" + I. tostring () + "/R/N"); threadpool. getminthreads (Out I, out J); response. write ("minimum working thread" + I. tostring () + "/R/N"); threadpool. getavailablethreads (Out I, out J); response. write ("available threads:" + I. tostring () + "/R/N"); thread. sleep (5000); watch. stop (); decimal Tim = watch. elapsed. ticks/10 m; response. write ("time spent:" + Tim. tostring ());

 

 

Note: according to the test, the minimum number of threads is the same as that described on msdn. The default value is 1, but the maximum number of threads should be Windows applications (CPU x 250 ), web applications (CPU x 100), So If AutoConfig = "true" is not 20 or 25, but 100, the test results are consistent on different machines.

 

Ii. Significance of Optimization

According to the official statement, the creation of threads consumes a lot of system resources. Therefore, when the user traffic is very high, the number of minworkerthreads and other configurations should be increased, and AutoConfig = "false"

Modify to false, and specify maxworkerthreads = "20" minworkerthreads = "10" at the same time. In the above Code, the thread will sleep for 5 seconds and press 500 concurrent threads, in the second half of the test, we found that the number of available threads (maximum number of threads-Number of threads in use) reached about 9. After the concurrency is stopped, the number of available threads can be restored after a period of time, I thought the setting range was large, and I re-set maxworkerthreads = "10" minworkerthredad = "5". I pressed it again and found that the number of available threads was around 7, it is not exhausted, so it is initially believed that threadpool has its own internal protection mechanism. A certain number of available threads should be reserved to deal with other processing.

 

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.