ThreadPool (thread pool) is a static class, it does not define any Constructor (), we can only use its static method, this is because ThreadPool is a managed thread pool, is managed by CLR.
ThreadPool uses the WaitCallback delegate, and all the work it needs to do is in the background. This makes it easier to queue and run work items. It can pass a State object (providing data) to the worker thread ). The State object is private and has a scope at the thread layer, so no synchronization is required.
ThreadPool aims to reduce the thread initialization overhead and implement parallel processing .. The ThreadPool in the NET class library is the basis of asynchronous IO, for example, in the System. net. in the Socket, we can use BeginAccept, EndAccept to put the Socket operation to be blocked in the system thread pool for running, and notify the main thread after the execution ends.
The threads registered in a ThreadPool have the default stack size and default priority. They all exist in Multithreaded apartment.
The Thread in ThreadPool cannot be canceled manually or started manually. Therefore, ThreadPool does not apply to long threads. All you need to do is to delegate a WaitCallback to ThreadPool, and the rest of the work will be automatically completed by the system. The system starts threads one by one in the ThreadPool thread queue.
When the thread pool is full, redundant threads are queued in the queue. When the thread pool is idle, the system automatically drops into the waiting thread to maintain system utilization.
ThreadPool is used in our program for some time-consuming or congested operations. When you want to learn complex synchronization technologies, such as events, or call the Join method for a field table, the thread pool cannot meet the requirements. use a separate Thread instead of ThreadPool in the following cases:
1. Specify the detailed priority for the thread.
2. Thread execution takes a long time
3. Put the thread in a separate thread apartment.
4. perform operations on the thread during thread execution, such as interrupting or suspending the thread.
Generally, computing-intensive operations are run in the worker thread pool, and the thread pool size is automatically adjusted according to the current CPU usage. We can set the thread pool size using the following two methods:
ThreadPool. SetMaxThreads (10,200 );
ThreadPool. SetMinThreads (2, 40 );
The two parameters are WorkThread and IO thread respectively.
Let's take a look at a simple example (the running results won't be the same every time, which should be the normal response of the ThreadPool background processing)
Using System;
Using System. Threading;
Using System. Collections. Generic;
Using System. Text;
Namespace ThreadPoolDemo
...{
Class Program
...{
Static void Main (string [] args)
...{
For (int I = 0; I <20; I ++)
...{
ThreadPool. QueueUserWorkItem (
New WaitCallback (DoWork), I );
}
Console. ReadLine ();
}
Static void DoWork (object state)
...{
Int threadNumber = (int) state;
Console. WriteLine ("Thread {0} reporting for duty.", state );
Console. WriteLine ();
}
}
}
Next, we will consider how to use ThreadPool to schedule some jobs that run cyclically. NET provides the System. Threading. Timer class to implement this function. Timer and TimerCallback are involved. The latter is also a delegate, and its declaration is as follows:
Public delegate void TimerCallback (object state );
Obviously, its usage is exactly the same as that of WaitCallback above. We can simply change the above example to a periodic operation:
Timer tm = new Timer (new TimerCallback (DoWork), new testObject (), 0, 2000 );
The following two parameters are the start delay time and period.
The thread allocation mechanism of Timer is related to the time complexity of other Timer operations at the same time. When several timers are defined to work at the same time, if each operation takes a long time and may expire at the same time, the thread pool may define different execution threads for each Timer operation. For simple operations, multiple timers may be executed in the same thread.
Even if you haven't shown the ThreadPool method in the thread, as long as you are writing. NET program, you may already be using the thread pool. Do not believe it. Open. NET program. Check the number of threads in the task manager. You will find that N threads are running, even if you only use one thread. If you use an asynchronous API, the number of threads may make you stunned.