【多线程笔记】AsyncLocal、ThreadLocal
AsnyncLocal与ThreadLocal都是存储线程上下文的变量,但是,在实际使用过程中两者又有区别主要的表现在:
- AsyncLocal变量可以在父子线程中传递,创建子线程时父线程会将自己的AsyncLocal类型的上下文变量赋值到子线程中,但是,当子线程改变线程上下文中AsnycLocal变量值后,父线程不会同步改变。也就是说AsnycLocal变量只会影响他的子线程,不会影响他的父级线程。
- TreadLocal只是当前线程的上下文变量,不能在父子线程间同步。
AsyncLocal:
这个主要是用于保存异步等待上下文中的共享变量的值。从C# 5开始,引入了相当简便的异步等待语法,即await关键字调用异步方法,允许异步等待。
即代码在使用await关键字调用异步方法后,当前程序会等待异步方法返回后才会继续执行,但在这个等待过程中,不会阻塞当前线程,这比起编写委托来回调方便多了。
异步方法是基于Task的自动线程调度,在异步上下文的切换过程中,有可能会导致数据丢失。比如,在await调用前,对某个变量赋了值,而这个变量是多个线程共享的;当await调用返回后,有可能当前代码仍然处于先前的线程上,但也有可能被调度到其他线程上。这种情况一般发生在与应用程序UI线程无关的代码上,如果异步操作是由UI启动的,通常情况下不会调动异步上下文的线程,然而,如果异步操作是非UI触发的,典型的如在Main入口处启动的,这就很有可能出现异步上下文处于不同的线程上的情形。
using System;
using System.Threading;
using System.Threading.Tasks;
namespace await_aysnc
{
class Program
{
static ThreadLocal<int> ThreadObj = new ThreadLocal<int>();
static AsyncLocal<int> AsyncObj = new AsyncLocal<int>();
static void Main(string[] args)
{
AsyncObj.Value = 1;
ThreadObj.Value = 1;
Console.WriteLine($"Task执行前:AsyncObj= {AsyncObj.Value} ThreadObj= {ThreadObj.Value} ThreeadId = {Thread.CurrentThread.ManagedThreadId}");
Task.Run(async() =>
{
Console.WriteLine($"RunAsync异步执行前:AsyncObj= {AsyncObj.Value} ThreadObj= {ThreadObj.Value} ThreeadId = {Thread.CurrentThread.ManagedThreadId}");
await RunAsync();
Console.WriteLine($"RunAsync异步执行后:AsyncObj = {AsyncObj.Value} ThreadObj= {ThreadObj.Value} ThreeadId = {Thread.CurrentThread.ManagedThreadId}");
});
Console.WriteLine($"Task执行后:AsyncObj= {AsyncObj.Value} ThreadObj= {ThreadObj.Value} ThreeadId = {Thread.CurrentThread.ManagedThreadId}");
Console.Read();
}
static async Task RunAsync()
{
Console.WriteLine($"Delay异步执行前:AsyncObj = {AsyncObj.Value} ThreadObj= {ThreadObj.Value} ThreeadId = {Thread.CurrentThread.ManagedThreadId} ");
AsyncObj.Value = 2;
ThreadObj.Value = 2;
await Task.Delay(100);
Console.WriteLine($"Delay异步执行后:AsyncObj = {AsyncObj.Value} ThreadObj= {ThreadObj.Value} ThreeadId = {Thread.CurrentThread.ManagedThreadId}");
}
}
}
Task执行前:AsyncObj= 1 ThreadObj= 1 ThreeadId = 1
Task执行后:AsyncObj= 1 ThreadObj= 1 ThreeadId = 1
RunAsync异步执行前:AsyncObj= 1 ThreadObj= 0 ThreeadId = 3
Delay异步执行前:AsyncObj = 1 ThreadObj= 0 ThreeadId = 3
Delay异步执行后:AsyncObj = 2 ThreadObj= 0 ThreeadId = 4
RunAsync异步执行后:AsyncObj = 1 ThreadObj= 0 ThreeadId = 4
参考:
https://www.cnblogs.com/tcjiaan/p/5007737.html
http://www.mamicode.com/info-detail-2911670.html(AsyncLocal实现原理)