Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand AsnycLocal and ThreadLocal in C #

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to understand AsnycLocal and ThreadLocal in C #". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand AsnycLocal and ThreadLocal in C#".

Both AsnyncLocal and ThreadLocal are variables that store thread context, but the main differences between them in practice are as follows:

The AsyncLocal variable can be passed in the parent-child thread, and the parent thread assigns its own AsyncLocal-type context variable to the child thread when creating the child thread, but when the child thread changes the value of the AsnycLocal variable in the context of the thread, the parent thread does not change synchronously. In other words, the AsnycLocal variable only affects his child thread, not his parent thread.

TreadLocal is only a context variable for the current thread and cannot be synchronized between parent and child threads.

Using System;using System.Threading;using System.Threading.Tasks;namespace await_aysnc {class Program {static ThreadLocal ThreadObj = new ThreadLocal (); static AsyncLocal AsyncObj = new AsyncLocal (); static void Main (string [] args) {AsyncObj.Value = 1; ThreadObj.Value = 1 Console.WriteLine ($"before Task execution: AsyncObj= {AsyncObj.Value} ThreadObj= {ThreadObj.Value} ThreeadId = {Thread.CurrentThread.ManagedThreadId}"); Task.Run (async () = > {Console.WriteLine ($"before RunAsync Asynchronous execution: AsyncObj= {AsyncObj.Value} ThreadObj= {ThreadObj.Value} ThreeadId = {Thread.CurrentThread.ManagedThreadId}"); await RunAsync () Console.WriteLine ($"after RunAsync asynchronous execution: AsyncObj= {AsyncObj.Value} ThreadObj= {ThreadObj.Value} ThreeadId = {Thread.CurrentThread.ManagedThreadId}"); Console.WriteLine ($"after Task execution: AsyncObj= {AsyncObj.Value} ThreadObj= {ThreadObj.Value} ThreeadId = {Thread.CurrentThread.ManagedThreadId}"); Console.Read () } static async Task RunAsync () {Console.WriteLine ($"before Delay asynchronous execution: AsyncObj = {AsyncObj.Value} ThreadObj= {ThreadObj.Value} ThreeadId = {Thread.CurrentThread.ManagedThreadId}"); AsyncObj.Value = 2; ThreadObj.Value = 2; await Task.Delay (100) Console.WriteLine ($"after Delay asynchronous execution: AsyncObj = {AsyncObj.Value} ThreadObj= {ThreadObj.Value} ThreeadId = {Thread.CurrentThread.ManagedThreadId}");}

Before Task execution: AsyncObj= 1 ThreadObj= 1 ThreeadId = 1

After Task execution: AsyncObj= 1 ThreadObj= 1 ThreeadId = 1

Before RunAsync asynchronous execution: AsyncObj= 1 ThreadObj= 0 ThreeadId = 3

Before Delay asynchronous execution: AsyncObj = 1 ThreadObj= 0 ThreeadId = 3

After Delay asynchronous execution: AsyncObj = 2 ThreadObj= 0 ThreeadId = 4

After RunAsync is executed asynchronously: AsyncObj = 1 ThreadObj= 0 ThreeadId = 4

The following conclusions can be drawn from the results:

The ThreadLocal value is cut to 0 before RunAsync execution, that is, the ThreadLocal variable does not inherit the context variable of the main thread in the new thread, but AsyncLocal inherits it.

When Delay returns, the thread id changes, and the variable in TheadLocal is 0.

After the RunAsync ends, the AyncLocal switches back to the context value of the main thread, and the value in the same Threadlocal is lost.

Because after await returns, .net will randomly select a thread in the thread pool to execute the logic after await, so there is a certain chance that await will get the previous thread at the same time. If this happens, TreadLocal will get the previous context, and unexpected problems will occur at this time. From the phenomenon, reading the report error may be a probability problem.

Thank you for your reading, the above is the content of "how to understand AsnycLocal and ThreadLocal in C#". After the study of this article, I believe you have a deeper understanding of how to understand AsnycLocal and ThreadLocal in C#, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report