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.
|