C # thread -- third thread pool,

Source: Internet
Author: User

C # thread -- third thread pool,

Overview
The thread pool has the following advantages:

1. The thread pool in multithreading can reduce the number of threads we create and reuse the threads in the thread pool reasonably. Because threads with threads in the thread pool are in the pending task status.

2. You do not need to manage and maintain threads that have a short life cycle. You do not need to allocate resources to them when they are created, and release resources after they have executed the task.

3. The thread pool optimizes the threads in the pool according to the current system characteristics.

Disadvantages of thread pool:

After the task is handed over to the thread pool, the priority of the thread cannot be controlled, and some thread names and other information can be set. [However, we can add a layer before putting it into the thread pool to improve this work]

Thread Pool parameter settings

Sample Code:

Int workerThreads, completionPortThreads; ThreadPool. getMaxThreads (out workerThreads, out completionPortThreads); Console. writeLine ("Maximum number of auxiliary threads in the thread pool: {0 }. maximum number of asynchronous I/O threads in the thread pool: {1} ", workerThreads, completionPortThreads); ThreadPool. getMinThreads (out workerThreads, out completionPortThreads); Console. writeLine ("the minimum number of auxiliary threads created by the thread pool based on your needs: {0 }. thread Pool: {1} ", workerThreads, completionPortThreads); // set the thread pool default parameter ThreadPool. setMaxThreads (100,100); ThreadPool. setMinThreads (2, 2); ThreadPool. getMaxThreads (out workerThreads, out completionPortThreads); Console. writeLine ("Maximum number of auxiliary threads in the thread pool: {0 }. maximum number of asynchronous I/O threads in the thread pool: {1} ", workerThreads, completionPortThreads); ThreadPool. getMinThreads (out workerThreads, out completionPortThreads); Console. writeLine ("the minimum number of auxiliary threads created by the thread pool based on your needs: {0 }. thread Pool: {1} ", workerThreads, completionPortThreads); ThreadPool. getAvailableThreads (out workerThreads, out completionPortThreads); Console. writeLine ("number of available auxiliary threads: {0 }. number of available asynchronous I/O threads: {1} ", workerThreads, completionPortThreads );

Output result:

1. Maximum number of CLR thread pools: 1023. Maximum number of I/O thread pools: 1000.

2. We can modify the maximum number and minimum number of thread pools.

[Msdn recommendation: do not set the number of auxiliary threads or the number of I/O completion threads to smaller than the number of processors on the computer.

If the host is running in a common language, such as Internet Information Service (IIS) or SQL Server, the host may restrict or prohibit the change of the thread pool size.

Exercise caution when changing the maximum number of threads in the thread pool. Although such changes may be beneficial to your code, they may have adverse effects on the code library you are using.

Setting the thread pool size too large may cause performance problems. If too many threads are executed at the same time, the overhead of task switching becomes a major factor affecting performance.]

Use of Thread Pool

Common Methods of thread pool:

public static bool QueueUserWorkItem(WaitCallback callBack, object state);

WaitCallback: the callback method to be executed by the thread pool thread.

[ComVisible(true)]public delegate void WaitCallback(object state);

Sample Code:

ThreadPool. setMaxThreads (12, 12); int workerThreads, completionPortThreads; ThreadPool. getMaxThreads (out workerThreads, out completionPortThreads); Console. writeLine ("Maximum number of auxiliary threads in the thread pool: {0 }. maximum number of asynchronous I/O threads in the thread pool: {1} ", workerThreads, completionPortThreads); ThreadPool. getMinThreads (out workerThreads, out completionPortThreads); Console. writeLine ("the minimum number of auxiliary threads created by the thread pool based on your needs: {0 }. thread Pool: {1} ", workerThreads, completionPortThreads); Stopwatch stopwatch = new Stopwatch (); stopwatch. start (); WaitCallback callback = index => {Console. writeLine (String. format ("{0}: Task {1} started", stopwatch. elapsed, index); Thread. sleep (1, 10000); Console. writeLine (String. format ("{0}: Task {1} finished", stopwatch. elapsed, index) ;}; for (int I = 0; I <20; I ++) {ThreadPool. queueUserWorkItem (callback, I);} Console. read ();

Output result:

00:00:00. 0009109: Task 0 started 2 00:00:00. 0011152: Task 2 started 3 00:00:00. 0010331: Task 1 started 4 00:00:00. 0013977: Task 3 started 5 00:00:01. 0640656: Task 4 started 6 00:00:01. 5959091: Task 5 started 7 00:00:02. 1282115: Task 6 started 8 00:00:02. 6604640: Task 7 started 9 00:00:03. 1919942: Task 8 started10 00:00:03. 7241812: Task 9 started11 00:00:04. 2562930: Task 10 started12 00:00:04. 7883300: Task 11 started13 00:00:10. 0337174: Task 0 finished14 00:00:10. 0337912: Task 2 finished15 00:00:10. 0341861: Task 3 finished16 00:00:10. 0343205: Task 13 started17 00:00:10. 0342149: Task 12 started18 00:00:10. 0345326: Task 1 finished19 00:00:10. 0347520: Task 14 started20 00:00:10. 9400517: Task 15 started21 00:00:11. 0639205: Task 4 finished22 00:00:11. 0643085: Task 16 started23 00:00:11. 5960161: Task 5 finished24 00:00:11. 5966256: Task 17 started25 00:00:12. 1279212: Task 6 finished26 00:00:12. 1294851: Task 18 started27 00:00:12. 6609840: Task 7 finished28 00:00:12. 6613285: Task 19 started29 00:00:13. 1921462: Task 8 finished30 00:00:13. 7240561: Task 9 finished31 00:00:14. 2560682: Task 10 finished32 00:00:14. 7880441: Task 11 finished33 00:00:20. 0342193: Task 13 finished34 00:00:20. 0354372: Task 14 finished35 00:00:20. 0343117: Task 12 finished36 00:00:20. 9402809: Task 15 finished37 00:00:21. 0662233: Task 16 finished38 00:00:21. 5983967: Task 17 finished39 00:00:22. 1293673: Task 18 finished40 00:00:22. 6623133: Task 19 finishedView Code

The minimum number of threads in the thread pool: when the minimum number of threads in the thread pool is reached, the thread pool starts to create threads ).
Between and 04 seconds, an average of one second is used to create a thread. There are no more than two threads created in the thread pool per second.
At, the number of threads in the thread pool has reached the maximum, and a new thread cannot be created.
At, the thread pool thread ID0-4 has completed the task, and the thread pool has started to re-create the thread (thread ID: 12-15) a total of 4.

CLR thread pool and IO Thread Pool
Will the normal use of the IO thread pool be affected when the CLR thread pool is full?

Sample Code:

ThreadPool. setMaxThreads (5, 5); ThreadPool. setMinThreads (5, 5); int workerThreads, completionPortThreads; ThreadPool. getMaxThreads (out workerThreads, out completionPortThreads); Console. writeLine ("Maximum number of auxiliary threads in the thread pool: {0 }. maximum number of asynchronous I/O threads in the thread pool: {1} ", workerThreads, completionPortThreads); ManualResetEvent waitHandle = new ManualResetEvent (false); Stopwatch watch = new Stopwatch (); watch. start (); // IO thread pool WebRequest request = HttpWebRequest. create ("http://www.taobao.com/"); request. beginGetResponse (ar => {var response = request. endGetResponse (ar); Console. writeLine (watch. elapsed + ": Response Get") ;}, null); // CLR thread pool for (int I = 0; I <10; I ++) {ThreadPool. queueUserWorkItem (index => {Console. writeLine (String. format ("{0}: Task {1} started", watch. elapsed, index); waitHandle. waitOne (); // blocking thread pool}, I);} Console. read ();

Output result:

1. Set the maximum number of threads in the CLR thread pool and the maximum number of threads in the IO thread pool to: 5.

2. Add 10 thread tasks to the CLR thread pool and block the thread pool. At last, only the running information of five threads is printed in the output result. The CLR thread pool has reached the maximum number of threads, and the IO thread still has response data.

It is proved that when the CLR thread pool is full, the use of the IO thread pool will not be affected.

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.