Ant*_*hin 5 .net c# multithreading
在第27章,CLR通过由Richter撰写的c#书中有一个声明:
您可以使用ExecutionContext类来抑制执行上下文的流动,从而提高应用程序性能.对于服务器应用程序,性能提升可能非常大.
其次是示例:
CallContext.LogicalSetData("Name", "Jeffrey");
//Initiate some work to be done by thread pool
ThreadPool.QueueUserWorkItem(
state => Console.WriteLine("Name={0}", CallContext.LogicalGetData("Name")
));
Run Code Online (Sandbox Code Playgroud)
其次是第二部分:
//Now suppress the flowing of the Main thread's execution context
ExecutionContext.SuppressFlow();
//Initiate some work to be done by thread pool
ThreadPool.QueueUserWorkItem(
state => Console.WriteLine("Name={0}", CallContext.LogicalGetData("Name")
));
ExecutionContext.RestoreFlow();
Run Code Online (Sandbox Code Playgroud)
结果:
Name=Jeffrey
Name=
Run Code Online (Sandbox Code Playgroud)
子线程已经失去了它的上下文(在示例的第二部分中),因为副作用参数在压抑后丢失.但是我们节省了CPU时间(在我的情况下,节省了大约10%的CPU时间).
| 归档时间: |
|
| 查看次数: |
868 次 |
| 最近记录: |