AsyncLocal<T>与ThreadLocal<T>区别研究
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace ATLearn
{
/// <summary>
/// Guid工厂
/// </summary>
static class GuidFactory
{
private static ThreadLocal<string> _localGuid = new ThreadLocal<string>();
//private static AsyncLocal<string> _localGuid = new AsyncLocal<string>();
public static string CurrentGuid
{
get
{
if (_localGuid.Value == null)
{
_localGuid.Value = Guid.NewGuid().ToString();
}
return _localGuid.Value;
}
}
} class APP
{
private Task<int> awaitFunction()
{
return Task.Run<int>(() =>
{
Console.WriteLine("awaitFunction:" + GuidFactory.CurrentGuid);
return ;
});
} private async void asyncFunction()
{
Console.WriteLine("asyncFunction:" + GuidFactory.CurrentGuid);
int num = await awaitFunction();
Console.WriteLine("asyncFunction:" + GuidFactory.CurrentGuid);
} public APP()
{
asyncFunction();
asyncFunction(); while (true)
{
Thread.Sleep();
}
}
} class Program
{
static void Main(string[] args)
{
APP App = new APP();
}
}
}

/// <summary>
/// Guid工厂
/// </summary>
static class GuidFactory
{
//private static ThreadLocal<string> _localGuid = new ThreadLocal<string>();
private static AsyncLocal<string> _localGuid = new AsyncLocal<string>();
public static string CurrentGuid
{
get
{
if (_localGuid.Value == null)
{
_localGuid.Value = Guid.NewGuid().ToString();
}
return _localGuid.Value;
}
}
}

AsyncLocal<T>与ThreadLocal<T>区别研究的更多相关文章
- Spring配置中的"classpath:"与"classpath*:"的区别研究(转)
Spring配置中的"classpath:"与"classpath*:"的区别研究(转) 概念解释及使用场景: classpath是指WEB-INF文件夹下 ...
- CallContext,ThreadStatic,AsyncLocal<T>,ThreadLocal<T>,学习笔记
1.CallContext 在当前调用上下文的线程数据槽里存储对象 2.ThreadStatic 是一个特性 3.AsyncLocal<T> 是一个类型,该字段应当为static,保证单例 ...
- ES5/ES6的区别研究(ECMAScript)
我所理解的概念应该是语法的区别和特性的区别 这里是ECMAScript的解析http://baike.baidu.com/item/ECMAScript 参考: (ES6)http://es6.rua ...
- 关于offsetTop offsetHeight clientHeight scrollHeight scrollTop的区别研究
我是以chrome浏览器做的研究. 先看一段代码: <script> window.addEventListener('DOMContentLoaded',function(){ var ...
- Spring配置中的"classpath:"与"classpath*:"的区别研究
概念解释及使用场景: classpath是指WEB-INF文件夹下的classes目录. 通常我们一般使用这种写法实在web.xml中,比如spring加载bean的上下文时,如下: <!--系 ...
- 浅谈AsyncLocal,我们应该知道的那些事儿
前言 最近查看有关框架源码,发现AsyncLocal这玩意水还挺深,于是花了一点功夫去研究,同时对比ThreadLocal说明二者区别以及在何时场景下使用AsyncLocal或ThreadLocal. ...
- AsyncLocal<T>在链路追踪中的应用
前言 在项目生产中日志的记录是必不可少的,在.net项目中,要说日志组件,log4net绝对可有一席之地,随着公司业务的发展,微服务则必定无可避免.在跨服务中通过日志进行分析性能或者排查故障点,如何快 ...
- ThreadLocal原理及其实际应用
前言 java猿在面试中,经常会被问到1个问题: java实现同步有哪几种方式? 大家一般都会回答使用synchronized, 那么还有其他方式吗? 答案是肯定的, 另外一种方式也就是本文要说的Th ...
- ThreadLocal (二):什么时候使用 InheritableThreadLocal
一.ThreadLocal 在父子线程传递的问题 public class InheritableThreadLocalDemo { // 全局变量 // static ThreadLocal< ...
随机推荐
- The error may exist in com/bjpowernode/dao/StudentDao.xml ### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderExcept
The error may exist in com/bjpowernode/dao/StudentDao.xml### Cause: org.apache.ibatis.builder.Builde ...
- CA认证的原理和流程及https原理
1.什么是CA证书. 看过一些博客,写的比较形象具体. ◇ 普通的介绍信 想必大伙儿都听说过介绍信的例子吧?假设 A 公司的张三先生要到 B 公司去拜访,但是 B 公司的所有人都不认识他,他咋办捏?常 ...
- jquery实现全选 反选 取消
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 小程序报错:request:fail错误(含https解决方案)(真机预览问题)
问题描述:域名已经备案,我全部都有,也在后台配置了,但是手机预览,还是请求失败, PC端是可以请求数据出来的 新版开发者工具增加了https检查功能:可使用此功能直接本地避开ssl协议版本检查,但是此 ...
- Caused by: java.lang.ClassNotFoundException: Didn't find class "io.grpc.helloworldexample.HelloworldActivity" on path: DexPathList
FAQ: Android app 编译好后安装到手机,运行时闪退,报如下错误: java.lang.RuntimeException: Unable to instantiate activity ...
- Unable to locate package python-pip
原文:https://blog.csdn.net/yyinhai/article/details/53056973 Ubuntu下执行apt install python-pip得到如下错误提示: R ...
- MVC区域area
1.当项目业务比较庞大,可以通过区域来分拆. 2.添加区域时,默认会生成一下文件. 3.Application_Start()必需含有AreaRegistration.RegisterAllAreas ...
- PyTorch中的backward [转]
转自:https://sherlockliao.github.io/2017/07/10/backward/ backward只能被应用在一个标量上,也就是一个一维tensor,或者传入跟变量相关的梯 ...
- kudu的读取数据流程
当客户端从Kudu的表中读取数据时,必须首先建立需要连接的系列tablet服务器. 通过执行tablet发现过程(如上所述)来确定包含要读取的主关键字范围的tablet的位置(读取不必在领导者tabl ...
- Codeforces 873F Forbidden Indices 字符串 SAM/(SA+单调栈)
原文链接https://www.cnblogs.com/zhouzhendong/p/9256033.html 题目传送门 - CF873F 题意 给定长度为 $n$ 的字符串 $s$,以及给定这个字 ...