Let's talk about the thread pool today:
Applications can have multiple threads, which consume a lot of time to wait for an event to occur in sleep state. Other threads may enter sleep state, and are only regularly awakened to change or update status information in a round robin manner, and then enter sleep state again. To simplify the management of these threads ,. NET Framework provides a thread pool for each process. A thread pool has several waiting operation statuses. When a waiting operation is completed, the auxiliary thread in the thread pool executes the callback function. Threads in the thread pool are managed by the system. programmers can concentrate on application tasks without having to perform thread management. A thread pool is provided through the ThreadPool class in the base class library. This thread pool can be used to send work cash streams and process asynchronous I/O, representing other threads waiting and processing timers. all methods of the ThreadPool class are static methods. threadPool itself is also a static class.
Let's take a look at his definition and some common methods:
Public static class ThreadPool
{
[SecuritySafeCritical]
Public static void GetAvailableThreads (out int workerThreads, out int completionPortThreads );
[SecuritySafeCritical]
Public static void GetMaxThreads (out int workerThreads, out int completionPortThreads );
[SecuritySafeCritical]
Public static void GetMinThreads (out int workerThreads, out int completionPortThreads );
[SecuritySafeCritical]
Public static bool QueueUserWorkItem (WaitCallback callBack );
[SecuritySafeCritical]
Public static bool QueueUserWorkItem (WaitCallback callBack, object state );
}
The difference between the maximum number of threads in the thread pool returned by the GetAvailableThreads method and the number of active threads. WorkerThreads: number of available auxiliary threads. CompletionPortThreads: number of available asynchronous I/O threads.
The GetMaxThreads method obtains the maximum number of auxiliary threads maintained by the thread pool. Parameter workerThreads: Maximum number of auxiliary threads in the thread pool. CompletionPortThreads: the maximum number of idle asynchronous I/O threads maintained by the thread pool.
The GetMinThreads method obtains the minimum number of idle auxiliary threads maintained by the thread pool. workerThreads: the minimum number of idle auxiliary threads maintained by the thread pool. completionPortThreads: the minimum number of idle asynchronous I/O threads maintained by the thread pool.
The QueueUserWorkItem method queues methods for execution. The callBack parameter indicates the method to be executed. state: the object that contains the data used by the method.
The following is an example:
1 using System;
2 using System. Collections. Generic;
3 using System. Linq;
4 using System. Text;
5 using System. Threading;
6
7 namespace ConsoleApplication6
8 {
9 class Program
10 {
11 static void Main (string [] args)
12 {
13 Console. WriteLine ("using the main thread for asynchronous entry ");
14
15 AutoResetEvent asyncOpIsDone = new AutoResetEvent (false );
16 ThreadPool. QueueUserWorkItem (new WaitCallback (x) =>
17 {
18
19 Thread. Sleep (1000 );
20 Console. WriteLine ("job ");
21
22 }));
23 string s = "My Parameters ";
24 ThreadPool. QueueUserWorkItem (new WaitCallback (x) =>
25 {
26
27 Thread. Sleep (2000 );
28 Console. WriteLine ("Job 1 ");
29 Console. WriteLine (x );
30
31}), s );
32 ThreadPool. QueueUserWorkItem (new WaitCallback (MyAsyncOperation), asyncOpIsDone );
33 Console. WriteLine ("other main thread execution ");
34 Console. WriteLine ("the main thread waits for the task to finish processing ");
35 asyncOpIsDone. WaitOne ();
36}
37 static void MyAsyncOperation (Object state)
38 {
39
40 Thread. Sleep (5000 );
41 Console. WriteLine ("job 2 ");
42 (AutoResetEvent) state). Set ();
43}
44}
45}
The running result is www.2cto.com.
Here, there is a class AutoResetEvent, which is used to notify the waiting thread that an event has occurred. It is inherited from EventWaitHandle! In this example, two different calling methods are used, and the second one is parameter passing! Worker thread 1 passes the "My Parameters" string! When worker thread 2 is finished, an event will be reported to the waiting thread. here, when AutoResetEvent is initialized, the signal is set to false first, and worker thread 2 is set to true if the call is successful. asyncOpIsDone. waitOne () blocks the current thread until it receives a signal!
Let's look at the example below.
1 using System;
2 using System. Collections. Generic;
3 using System. Linq;
4 using System. Text;
5 using System. Threading;
6
7 namespace ConsoleApplication7
8 {
9 class Program
10 {
11 static void Main (string [] args)
12 {
13 Console. WriteLine ("\ n current Thread's HashCode: {0}", Thread. CurrentThread. GetHashCode (). ToString ());
14 Console. WriteLine ("current application domain name: {0}", AppDomain. CurrentDomain. FriendlyName );
15 int workerThreads;
16 int completionPortThreads;
17
18 AutoResetEvent asyncOpIsDone = new AutoResetEvent (false); // The signal is empty initially.
19 AutoResetEvent asyncOpIsDone2 = new AutoResetEvent (false );
20 ThreadPool. QueueUserWorkItem (new WaitCallback (MyWork), asyncOpIsDone );
21 ThreadPool. QueueUserWorkItem (new WaitCallback (MyWork1), asyncOpIsDone2 );
22 ThreadPool. GetMaxThreads (out workerThreads, out completionPortThreads );
23 Console. WriteLine ("Maximum number of auxiliary threads maintained by the thread pool: {0}", workerThreads );
24 ThreadPool. GetMinThreads (out workerThreads, out completionPortThreads );
25 Console. WriteLine ("minimum number of idle auxiliary threads maintained by the thread pool: {0}", workerThreads );
26/* WaitHandle [] waithandles = new WaitHandle [2];
27 waithandles [0] = asyncOpIsDone;
28 waithandles [1] = asyncOpIsDone2;
29 WaitHandle. WaitAll (waithandles );*/
30 // or you can
31 asyncOpIsDone. WaitOne ();
32 Console. WriteLine ("MyWork ended ");
33 asyncOpIsDone2.WaitOne ();
34 Console. WriteLine ("MyWork1 ended ");
35}
36 static void MyWork (Object state)
37 {
38 Console. WriteLine ("\ n current Thread's HashCode: {0}", Thread. CurrentThread. GetHashCode (). ToString ());
39 Console. WriteLine ("current application domain name: {0}", AppDomain. CurrentDomain. FriendlyName );
40 Thread. Sleep (2000 );
41 (AutoResetEvent) state). Set (); // indicates that the work has been completed.
42}
43 static void MyWork1 (Object state)
44 {
45 Console. WriteLine ("\ n current Thread's HashCode: {0}", Thread. CurrentThread. GetHashCode (). ToString ());
46 Console. WriteLine ("current application domain name: {0}", AppDomain. CurrentDomain. FriendlyName );
47 Thread. Sleep (1000 );
48 (AutoResetEvent) state). Set (); // indicates that the work has been completed.
49}
50
51}
52}
The running result is
In this example, you can see in detail the use of AutoRestEvent MyWork1 actually ended first, but asyncOpIsDone needs to wait for the signal of MyWork! So I output MyWork first. Here is another thing I want to talk about: WaitHandle. The above example has provided his usage! It is the base class of AutoRsetEvent.
There is also ThreadPool, which has a function RegisterWaitForSingleObject. This function is quite interesting! I will not give an example here!
Now, it's today!
From ENUO