Brief introduction:
The creation of a managed thread requires thousands of CPU cycles, and there is a noticeable overhead when a thread switch occurs. It is a way to improve the efficiency of the system to consider the reuse of threads and avoid repeatedly creating new threads.
The thread pool is an efficient way to create threads that are managed by the thread pool engine, and the developer will encapsulate the business or operations that need to be handled into a "work item" work item that is passed to the thread pool queue, picked and executed by different worker threads in the thread pools. When execution is complete, the thread is not destroyed, but continues to select other work items.
Code:
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Threading;namespacesample8_1_threadpool_basic{classProgram { Public Static voidWork1 () {System.Console.WriteLine ("ThreadWork1 Run {"); for(inti =0; I < -; i++) {System.Console.WriteLine ("THREADWORK1 * * * *:"+i); } System.Console.WriteLine ("ThreadWork1 Run}"); } Public Static voidWork2 () {System.Console.WriteLine ("ThreadWork2 Run {"); for(inti =0; I < -; i++) {System.Console.WriteLine ("THREADWORK2 =====:"+i); } System.Console.WriteLine ("ThreadWork2 Run}"); } Static voidMain (string[] args) {ThreadPool.QueueUserWorkItem (stat)={Work1 (); }); ThreadPool.QueueUserWorkItem (STAT)={WORK2 (); }); intA; intb; Threadpool.getminthreads ( outA outb); Console.WriteLine ("a="+a+"; b="+b); Console.ReadLine (); } }}
C # Parallel Programming----Threadpool