Creating too many threads increases the utilization of operating system resources and also handles resource requirements and potential occupancy conflicts. and using multithreading will complicate the process of code execution and resource competition, and a little attention will create bugs.
The advantages of using the thread pool compared to using a single thread are as follows:
1. Reduce application response time. Without creating a thread because threads in the thread pool are waiting to be assigned a task state
2. You do not have to manage and maintain a thread that has a short life cycle, and you do not have to assign resources to it when it is created, freeing resources after it finishes its task.
3. The thread pool optimizes the threads within the pool based on the current system characteristics.
Purpose the thread pool is used to reduce the overhead of creating and destroying threads.
. NET has a class ThreadPool that provides thread pool management.
ThreadPool is a static class, it has no constructors, and the externally provided functions are all static. There is one QueueUserWorkItem method, there are two overloaded forms
public static bool QueueUserWorkItem (WaitCallback CallBack): The method is queued for execution. This method executes when the thread pool threads become available.
public static bool QueueUserWorkItem (WaitCallback callback,object State): The method is queued for execution, and the data object that contains the method is specified. This method executes when the thread pool threads become available.
Public classthreadpooldemo{ PublicThreadPoolDemo () {} Public voidWork () {ThreadPool.QueueUserWorkItem (NewWaitCallback (countprocess)); ThreadPool.QueueUserWorkItem (NewWaitCallback (getenvironmentvariables)); }Private voidCountprocess (ObjectState ) {process[] Processes=process.getprocesses ();foreach(Process Pinchprocesses) {Console.WriteLine ("id:{0},processname:{1},starttime:{2}", P.id,p.processname,p.starttime); } Console.WriteLine ("Get process information complete"); }Private voidGetenvironmentvariables (ObjectState ) {IDictionary list=System.Environment.GetEnvironmentVariables ();foreach(DictionaryEntry Iteminchlist) {Console.WriteLine ("Key={0},value={1}", item. Key,item. Value); } Console.WriteLine ("Get system Variable information complete"); } Public voidBegin () {work (); Thread.Sleep ( the); Console.WriteLine ("OK"); Console.ReadLine (); }}
Disadvantages of the thread pool:
1, once added to the thread pool there is no way to stop it, unless the task is completed automatically stop;
2, a process to share a thread pool;
3. The task to be performed must not have a return value (of course, the method to be executed in the thread is also not a return value, if the return value does need to be resolved by other techniques);
4, the priority of all tasks in the thread pool is the same, unable to set the priority of the task;
5, not suitable for long-term implementation of tasks (such as in the Windows service), is not suitable for large tasks;
6. You cannot set a stable association identifier for a thread, such as specifying a name or other property for a thread in a thread pool that performs a particular task.
C # thread pool