Introduce the concept of thread pool first:
Baidu Encyclopedia : The thread pool is a form of multithreading that adds tasks to the queue during processing, and then starts these tasks automatically after the thread is created. Thread pool threads are all background threads. Each thread uses the default stack size, runs at the default priority, and is in a multithreaded apartment. If a thread is idle in managed code, such as waiting for an event, the thread pool inserts another worker thread to keep all the processors busy. If all thread pool threads are always busy, but the queue contains pending work, the thread pool will create another worker thread after a period of time but the number of threads will never exceed the maximum value. Threads that exceed the maximum value can be queued, but they will not start until other threads have finished.
Thread Pool Main methods:
// Parameters: // workerthreads: // The number of new minimum worker threads to be created by the thread pool as needed. // / completionportthreads :/ /The number of new minimum idle asynchronous I/O threads to be created by the thread pool as needed.
//return Result:true if the change was successful; otherwise false. [SecuritySafeCritical] Public Static BOOLSetMinThreads (intWorkerThreads,intcompletionportthreads); //Parameters://workerthreads://The maximum number of worker threads in the thread pool. //completionportthreads://The maximum number of asynchronous I/O threads in the thread pool. //return Result:true if the change was successful; otherwise false. [SecuritySafeCritical] Public Static BOOLSetMaxThreads (intWorkerThreads,intcompletionPortThreads);
Let's look at a simple example:
Public classProgram {Const intCyclenum =Ten; Static voidMain (string[] args) {Threadpool.setminthreads (1,1); Threadpool.setmaxthreads (5,5); for(inti =1; I <= cyclenum; i++) {ThreadPool.QueueUserWorkItem (NewWaitCallback (Testfun), i.tostring ()); } Console.WriteLine ("Main thread Execution! "); Console.WriteLine ("The main thread ends! "); Console.readkey (); } Public Static voidTestfun (Objectobj) {Console.WriteLine (string. Format ("{0}: first {1} threads", DateTime.Now.ToString (), obj. ToString ())); Thread.Sleep ( the); } }
Attached results:
As can be seen here, the execution of thread constructor threads does not affect the running of the main course, although the thread pool can manage multi-threaded execution, but it cannot know when it terminates. At this time we can use the previous lights AutoResetEvent and ManualResetEvent to solve the problem, for this is not known to friends can see multi-thread AutoResetEvent.
The above code changes slightly, as follows:
Public classProgram {Const intCyclenum =Ten; StaticAutoResetEvent MyEvent =NewAutoResetEvent (false); Static voidMain (string[] args) {Threadpool.setminthreads (1,1); Threadpool.setmaxthreads (5,5); for(inti =1; I <= cyclenum; i++) {ThreadPool.QueueUserWorkItem (NewWaitCallback (Testfun), i.tostring ()); } Console.WriteLine ("Main thread Execution! "); Console.WriteLine ("The main thread ends! "); Myevent.waitone (); Console.WriteLine ("thread pool terminated! "); Console.readkey (); } Public Static voidTestfun (Objectobj) {Console.WriteLine (string. Format ("{0}: first {1} threads", DateTime.Now.ToString (), obj. ToString ())); Thread.Sleep ( the); if(obj. ToString () = =cyclenum.tostring ()) {Myevent.set (); } } }
Here, when all the threads in the thread pool are done, the event can be captured, and we can use this method to get the thread pool termination event, with the following results:
According to the sword white Source: C # Multithreading-thread pool (ThreadPool) This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility. If you have any questions or suggestions, please enlighten me, thank you very much.
C # Multithreading-thread pool (ThreadPool)