Thread Local Storage
usingSystem;usingSystem.Threading;usingSystem.Threading.Tasks;namespaceconsoleapptest{classProgram {Static voidMain (string[] args) {threaddataslottest.test (); } } /// <summary> ///Thread Local Storage/// </summary> classThreaddataslottest { Public Static voidTest () { for(vari =0; I <Ten; i++) {Thread.Sleep (Ten); Task.run (()= { varSlot = Thread.getnameddataslot ("Test"); if(Slot = =NULL) {Thread.allocatenameddataslot ("Test"); } if(Thread.getdata (slot) = =NULL) {Thread.setdata (slot, DateTime.Now.Millisecond); } Console.WriteLine (Thread.CurrentThread.ManagedThreadId+":"+Thread.getdata (slot)); }); } console.readline (); } }}
If you are using a thread pool, it is best not to use this storage mechanism because the thread pool may not release used threads, resulting in possible sharing of data between multiple executions (data that is stored locally by the thread can be reset before each execution).
Invoke context
usingSystem;usingSystem.Runtime.Remoting.Messaging;usingSystem.Threading;usingSystem.Threading.Tasks;namespaceconsoleapptest{classProgram {Static voidMain (string[] args) {callcontexttest.test (); } } /// <summary> ///Invoke Context/// </summary> classCallcontexttest { Public Static voidTest () {if(CallContext.GetData ("Test") ==NULL) {Callcontext.setdata ("Test","Callcontext.setdata"); } for(vari =0; I <Ten; i++) {Thread.Sleep (Ten); Task.run (()= { if(CallContext.GetData ("Test") ==NULL) {Callcontext.setdata ("Test", DateTime.Now.Millisecond); } Console.WriteLine (Thread.CurrentThread.ManagedThreadId+":"+ CallContext.GetData ("Test")); }); } console.readline (); } }}
It is possible to know that each execution of the data is completely isolated and very much in line with our expectations. However, if we expect a child thread to be turned on during the call, how do we let the child thread access the data of the parent thread? This needs to be used to: "Logical call Context".
Logical call Context
usingSystem;usingSystem.Runtime.Remoting.Messaging;usingSystem.Threading;usingSystem.Threading.Tasks;namespaceconsoleapptest{classProgram {Static voidMain (string[] args) {executioncontexttest.test (); } } /// <summary> ///Invoke Context/// </summary> classExecutioncontexttest { Public Static voidTest () {Console.WriteLine ("Test: Callcontext.setdata"); Task.run (()={callcontext.setdata ("Test","Wolf"); Console.WriteLine (Thread.CurrentThread.ManagedThreadId+":"+ CallContext.GetData ("Test")); Task.run (()={Console.WriteLine (Thread.CurrentThread.ManagedThreadId+":"+ CallContext.GetData ("Test")); }); }); Thread.Sleep ( -); Console.WriteLine ("Test: Callcontext.logicalsetdata"); Task.run (()={callcontext.logicalsetdata ("Test","Wolf"); Console.WriteLine (Thread.CurrentThread.ManagedThreadId+":"+ Callcontext.logicalgetdata ("Test")); Task.run (()={Console.WriteLine (Thread.CurrentThread.ManagedThreadId+":"+ Callcontext.logicalgetdata ("Test")); }); Executioncontext.suppressflow (); Task.run (()={Console.WriteLine ("after Suppressflow:"+ Callcontext.logicalgetdata ("Test")); }); Executioncontext.restoreflow (); Task.run (()={Console.WriteLine ("after Restoreflow:"+ Callcontext.logicalgetdata ("Test")); }); }); Console.ReadLine (); } }}
Note Executioncontext.suppressflow (); and Xecutioncontext.restoreflow (), which can block propagation and reset propagation, respectively, which are allowed to propagate by default.
C # Thread Local Storage Call context logic call context