If you design a server program that starts a thread whenever a user request is processed, it will consume the server's resources on a certain program. One of the best ways to do this is to create some thread objects in advance before the server starts, and then, when processing client requests, get the thread objects from the built threads and process the request. The structure of saving these thread objects is called the thread pool.
It can be implemented in C # by the System.Threading.ThreadPool class, by default, ThreadPool up to 500 worker threads and 1000 I/O threads (depending on the number of machine CPUs and the. NET Framework version) , these data may vary). Here is an example of using C # to get threads from a thread pool:
private static void execute (object state)
{
Console.WriteLine (state);
}
static void Main (string[] args)
{
int workerthreads;
int completionportthreads;
Threadpool.getmaxthreads (out of workerthreads, out completionportthreads);
Console.WriteLine (workerthreads);
Console.WriteLine (completionPortThreads);
ThreadPool.QueueUserWorkItem (Execute, "thread 1"); Gets a thread from the thread pool and runs the Execute
ThreadPool.QueueUserWorkItem (Execute, "Thread 2");
ThreadPool.QueueUserWorkItem (Execute, "thread 3");
Console.ReadLine ();
}
The following figure shows the results of the above code.
Note that threads obtained using ThreadPool are background threads.