Abstract:
SeriesArticle, From a basicCodeSpeaking of this, we will gradually explore the mysteries of threadpool.
First, let's look at the thread pool:
It can be seen that the thread pool maintains one to n threads, and the operating system extracts requests from the request queue to allocate a thread pool suitable for thread processing.
Write the following code:
Using System;
Using System. Threading;
Public Class Threadbase
{
Public Static Void Main () {
System. Threading. waitcallback = New Waitcallback (mythreadwork );
Threadpool. queueuserworkitem (waitcallback, " First thread " );
Threadpool. queueuserworkitem (waitcallback, " Second thread " );
Threadpool. queueuserworkitem (waitcallback, " Third thread " );
Threadpool. queueuserworkitem (waitcallback, " Fourth thread " );
Console. Readline ();
}
Public Static Void Mythreadwork ( Object State) {< br> console. writeline ( " the thread starts now ...... {0} " , ( string ) State );
thread. sleep ( 10000 );
console. writeline ( " running ends ...... {0} " , ( string ) State );
}
}
Analyze the above Code:
1. Define a system. Threading. waitcallback object waitcallback.
Waitcallback is a delegate that indicates the callback method to be executed by the thread pool thread. Its prototype is as follows:[Comvisibleattribute (True)]
Public Delegate VoidWaitcallback (
Object state
)
1. There is a problem with the callback mechanism. The so-called callback is simply a function called by the operating system.ProgramNo need to call. It's like a repairer is at home, and you just need to tell him where the tools, such as pliers, screwdrivers, and tape, are to be repaired, don't worry about when and where he uses these repair tools.
2. The waitcallback parameter "object state", which contains the object of the information to be used by the callback method. I will talk about it in the following cases.
Since the prototype of the waitcallback delegate is the same, we declare a function similar to it. This function is what the thread needs to do.Public StaticVoidMythreadwork (ObjectState)
Here, there is a "static" in the function, which is because of the main relationship (more importantly, because of the C # Language Mechanism problem). If the waitcallback object is not in the static (static) method, this static is not required.
2. Add the method to be executed to the thread pool for execution by the operating system.
Here I have placed four methods for the operating system to execute:
Threadpool. queueuserworkitem (waitcallback, " First thread " );
Threadpool. queueuserworkitem (waitcallback, " Second thread " );
Threadpool. queueuserworkitem (waitcallback, " Third thread " );
Threadpool. queueuserworkitem (waitcallback, " Fourth thread " );
Here, the operations that I put into the thread pool are the same, and of course they can be different. Next, let's talk about them.
3. Block the main thread and wait for the thread to run in the thread pool.Console. Readline ();
If you ignore this code, you may not be able to see any output.
Well, this is what the main thread is doing. Next let's take a look at what the thread in the thread pool is doing.
This is very simple. It is to output the thread parameters, then sleep the thread for a period of time, and finally output the thread end information.