Thread Pool Basics
1. The thread pool maintains an operation request queue, appends the requested action to the thread pool queue, and the thread pool's code is extracted from the queue
An action item that is distributed to threads in the thread pool;
2.CLR initialization, there is no thread in the thread pool, when there are operations sent to the thread pool, if there are no threads or no
The idle state of the thread will create a new thread to perform the dispatch operation, and if there are idle threads, it will dispatch a
The idle state of the thread performs the operation;
3. After the thread pool thread completes the operation task, the threads are not destroyed, but instead return to the thread pool, enter the idle state, and wait for the response to another dispatch request;
4. When a thread pool is idle for a period of time (different CLR defines the time), the thread wakes itself up and terminates itself to free memory resources.
Example code:
1Console.WriteLine ("Main thread ...");2ThreadPool.QueueUserWorkItem (DoSomething,5);3Console.WriteLine ("The main thread continues to execute ...");4 5 Static voidDoSomething (ObjectState )6 {7Thread.Sleep ( -);8 intnum = (int) state;9Console.WriteLine ("dosomething ..., number:"+num);Ten return; One}
Output Result :
Main thread ...
The main thread continues to execute ...
DoSomething ..., Number:5.
Two execution contexts
The System.Threading namespace has a ExecutionContext class that allows you to control how the thread's execution context
From one thread "flow" to another, by default, the CLR automatically causes the initial thread's aspiration context to "flow" to any worker thread.
Example code:
1Callcontext.logicalsetdata ("Name","Mike");2 //Thread pool threads can access logical call context data3ThreadPool.QueueUserWorkItem (state = Console.WriteLine ("name:{0}", Callcontext.logicalgetdata ("Name")));4 5 //cancels the flow of execution context between asynchronous threads6 Executioncontext.suppressflow ();7 //thread pool threads will not be able to access logical call context data8ThreadPool.QueueUserWorkItem (state = Console.WriteLine ("name:{0}", Callcontext.logicalgetdata ("Name")));9 Ten //resumes the flow of execution contexts between asynchronous threads One Executioncontext.restoreflow (); A //...
Execution Result:
Name:mike
Name:
Three-Collaborative cancellation
1 //delegate invoked when registering to cancel Cancelationtoken2 varCTS =NewCancellationTokenSource ();3Cts. Token.register (() = Console.WriteLine ("The operation would be cancelled.--1"));4Cts. Token.register (() = Console.WriteLine ("The operation would be cancelled.--2"));5 6 //The thread starts to perform the operation7ThreadPool.QueueUserWorkItem (A + = Count (CTS). Token, +));8 9Console.WriteLine ("Please <Enter> to cancel the operation.");Ten console.readline (); One A //Communicate Cancellation Requests - CTS. Cancel (); - the - //Perform count operations - Static voidCount (CancellationToken token,intCountto) - { + for(inti =0; i < Countto; i++) - { + if(token.) iscancellationrequested)//whether cancellation has been requested A { at Break; - } -Thread.Sleep ( -); - Console.WriteLine (i); - } -}
description : Type the "Enter" key to communicate the cancellation ID, the count count operation ends, and the registered two delegate methods are executed.
(CLR via C # learning note) asynchronous operations-thread pool