This article is a mirror article of machine translation, please click here to jump to the original article.

View: 26065|Reply: 2

[Source] A brief analysis of the usage of .net/C# thread pool ThreadPool

[Copy link]
Posted on 6/21/2017 9:43:20 AM | | | |
What is a thread pool? Why use thread pools? How to use thread pools?

1. What is a thread pool?

        The .NET Framework's ThreadPool class provides a thread pool that can be used to execute tasks, send work items, handle asynchronous I/O, wait on behalf of other threads, and handle timers. So what is a thread pool? The thread pool is actually a "pool" that stores thread objects, and it provides some basic methods, such as setting the minimum/maximum number of threads in the pool, queuing the methods to be executed, etc. ThreadPool is a static class, so it can be used directly without creating objects.

2. Why use thread pools? What are the benefits?

        Microsoft's official website says the following: Many applications create a large number of threads that are asleep, waiting for events to occur. There are also many threads that may go into hibernation just to wake up periodically to poll for changed or updated state information. Thread pools, allowing you to use threads more efficiently through a pool of worker threads managed by the system.

        To put it simply, every new thread needs to occupy memory space and other resources, and with so many new threads, many of them are dormant or waiting for resources to be released. There are many threads that only do some small work periodically, such as refreshing data, etc., which are too wasteful to draw, and it is rare for a large number of threads to burst out in actual programming and then end in a short period of time. Therefore, the concept of thread pools was proposed. If the application makes another request to the thread pool, the thread in the pending state is activated and executes the task without creating a new thread, which saves a lot of overhead. The system will automatically destroy threads only when the number of threads reaches the maximum number of threads. Therefore, using thread pools can avoid large creation and destruction expenses, have better performance and stability, and secondly, developers can focus on other tasks by handing over threads to system management.


3. How do I use thread pools?

In fact, thread pools are very simple to use, as follows

a. Set the maximum and minimum thread pool:

ThreadPool.SetMaxThreads (int workerThreads,int completionPortThreads)
Set the number of requests for a pool of threads that can be active at the same time. All requests greater than this number will remain queued until the thread pool thread becomes available. You can also set the minimum number of threads.

b. Add tasks to the thread pool:

ThreadPool.QueueUserWorkItem(new WaitCallback(method name));

or
ThreadPool.QueueUserWorkItem(new WaitCallback(method name), parameter);

For example, if a maximum of 5 threads in the thread pool executes a method 60 times, the total salary for 5 years is as follows:



If you don't use thread pooling, you may have to open 60 threads to execute the Run() method asynchronously, which is a waste of space resources. Now we use up to 5 threads, which can be executed in 1 second, and the efficiency and performance are very good.





Previous:Baidu Map Full Screen Code (div full screen code)
Next:Alibaba Cloud's illegal URL blocking access processing notice
 Landlord| Posted on 6/21/2017 9:47:12 AM |

Thread pools have those advantages:

1. In multi-threading, the thread pool can reduce the number of threads we create, and reasonably reuse the threads in the thread pool. Because threads with threads in the thread pool are in a state of waiting to be assigned.

2. There is no need to manage and maintain threads with short lifespans, no need to allocate resources to them when they are created, and release resources after they complete their tasks.

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

Disadvantages of thread pools:

After we hand over the task to the thread pool to complete, we cannot control the priority of the thread, set some names of the threads, and other information. [But we can add a layer to refine this before putting it in the thread pool]

Posted on 12/6/2017 11:28:49 AM |
Mark it first, so you can learn later
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com