The thread pool is the basis for C # parallel development, where tasks in C #, asynchronous delegates, and so on, are used internally by thread pooling. All threads in the thread pool are background threads.
The thread pool, as its name implies, has a certain number of active threads for the program to invoke. In a Windows system, a process is a collection of program resources, a thread is a real execution unit, and if a host has multiple logical CPUs, the program can run in parallel. However, building a thread takes time, consumes memory, and can affect program performance if it is built and destroyed frequently. The thread pool mechanism can be a good remedy for this. When an asynchronous task is performed using a thread pool, the Task Scheduler puts the task into a thread in the thread pool and does not destroy the thread after the task completes, but continues to wait for other tasks for the user to execute. If a thread in a thread pool is performing a task, the thread pools create new threads.
//get the minimum number of threads intMinworkerthread, Minportthread; Threadpool.getminthreads ( outMinworkerthread, outminportthread); Console.WriteLine (minworkerthread.tostring ()+" "+minportthread.tostring ()); //get the maximum number of threads intMaxworkerthread, Maxportthread; Threadpool.getmaxthreads ( outMaxworkerthread, outmaxportthread); Console.WriteLine (maxworkerthread.tostring ()+" "+maxportthread.tostring ()); //gets the number of threads that can be established (that is, the maximum number of threads and the number of threads that already exist in the thread pool) intAvailableworkerthread, Availableportthread; Threadpool.getavailablethreads ( outAvailableworkerthread, outavailableportthread); Console.WriteLine (availableworkerthread.tostring ()+" "+ availableportthread.tostring ());
Thread pool threads are set up with the minimum number of threads and the maximum number of threads, and the minimum number of threads is typically equal to the number of logical CPUs. The maximum number of threads in. net4.0 is 32767 (worker thread), and (IO thread). The maximum number of threads and the minimum number of threads can be set. When the number of threads in the thread pool is less than the minimum number of threads, if a new thread is needed at this point, it is established immediately. When it is larger than the minimum number of threads, the thread pool waits 500 milliseconds to see if a new thread needs to be established. So the time to create a new thread is greater than 500 milliseconds. When the maximum number of threads has been reached, no new threads will be established. If there is a new task, the thread pool causes the task to wait until there are threads available. In addition, when thread pool threads are idle, they are automatically destroyed after a certain amount of time.
Thread pools are typically used to perform tasks that are less time-critical, start-of-execution time for tasks, and do not require strict execution order to maximize the benefits of the thread pool. In addition, when the minimum number of threads is exceeded, the new thread time is greater than 500 milliseconds, and the minimum thread size of the thread pool threads can be modified as needed to meet the actual needs of the program.
//only set up worker threads here//Set as needed intAppminthreadcount =5; //get the minimum number of threads intMinworkerthread, Minportthread; Threadpool.getminthreads ( outMinworkerthread, outminportthread); if(Appminthreadcount >minportthread) { varresult=threadpool.setminthreads (Appminthreadcount, Minportthread); //The return value is used to determine whether the setting is successfulConsole.WriteLine (Result); }
Understanding about the. NET Framework thread pool