当前位置: 首页 > news >正文

长沙哪家做网站设计好关键词搜索热度

长沙哪家做网站设计好,关键词搜索热度,怎么在wordpress上添加视频,网站开发要学习路线【多线程开发 2】从代码到实战TransmittableThreadLocal 本文将从以下几个点讲解TransmittableThreadLocal(为了方便写以下简称ttl): 前身 是什么? 可以用来做什么? 源码原理 实战 前身 ThreadLocal 要了解ttl就要先了解Java自带的类…

【多线程开发 2】从代码到实战TransmittableThreadLocal

本文将从以下几个点讲解TransmittableThreadLocal(为了方便写以下简称ttl):

  • 前身

  • 是什么?

  • 可以用来做什么?

  • 源码原理

  • 实战

前身

ThreadLocal

要了解ttl就要先了解Java自带的类ThreadLocal,threadlocal是作为当前线程中属性ThreadLocalMap集合中的某一个Entry的key值Entry(threadlocl,value),虽然不同的线程之间threadlocal这个key值是一样,但是不同的线程所拥有的ThreadLocalMap是独一无二的,,用于存储一些线程不安全的公共变量,通过“给每一个线程一个线程不安全的变量的拷贝”,来达到线程安全的目的,就不会出现变量多个线程之间共享的问题。

ThreadLocal 变量通常被private static修饰。当一个线程结束时,它所使用的所有 ThreadLocal 相对的实例副本都可被回收。

InheritableThreadLocal

使用ThreadLocal可以解决线程安全问题,但是也有一定的局限性,比如无法在父子线程之间传递信息,因此InheritableThreadLocal就是JDK为了解决这个问题而创建的

TTL是什么?可以做什么?

在现在开发的情况下肯定是需要复用线程的,如果说InheritableThreadLocal在生成子进程的时候会做信息传递,但是在使用线程池或者其他需要复用线程的地方,由于会不产生新的Thread,而是直接使用空闲的已建成的Thread,所以的话InheritableThreadLocal有时候会不能用,此时可以通过使用TTL解决对应问题。

img

读取线程间传递的ThreadLocal 值比较麻烦,ThreadLocal 和 InheritableThreadLocal 都没有开放内部的 ThreadLocalMap,不能直接读取。所以TTL继承了InheritableThreadLocal,在每次调用 ThreadLocal的 set/get/remove 等接口的时候,为 Thread 记录到底绑定了哪些需要发生线程间传递的 ThreadLocal 对象。

在创建runnable的时候,TTL会通过holder遍历全部的TTLRunnable快照,看出上下文中有哪些线程上的信息需要进行复制。

TTL的GitHub项目地址:https://github.com/alibaba/transmittable-thread-local,感兴趣的话可以查看一下源码,后续有时间的话我会出一篇详细解析TTL源码的项目地址。

实战

其中需要使用拦截器辅助实现,需要读者依据相应技术架构自行实现

使用TTL实现MDC日志在多系统之间的信息传输

public class CustomMdcAdapters implements MDCAdapter {private static final int WRITE_OPERATION = 1;private static final int MAP_COPY_OPERATION = 2;private static CustomMdcAdapters mtcMDCAdapter;static {mtcMDCAdapter = new CustomMdcAdapters();MDC.mdcAdapter = mtcMDCAdapter;}private final ThreadLocal<Map<String, String>> copyOnInheritThreadLocal = new TransmittableThreadLocal<>();private final ThreadLocal<Integer> lastOperation = new ThreadLocal<>();public static MDCAdapter getInstance() {return mtcMDCAdapter;}private static boolean wasLastOpReadOrNull(Integer lastOp) {return lastOp == null || lastOp == MAP_COPY_OPERATION;}private Integer getAndSetLastOperation(int op) {Integer lastOp = lastOperation.get();lastOperation.set(op);return lastOp;}private Map<String, String> duplicateAndInsertNewMap(Map<String, String> oldMap) {Map<String, String> newMap = Collections.synchronizedMap(new HashMap<>(16));if (oldMap != null) {// we don't want the parent thread modifying oldMap while we are// iterating over itsynchronized (oldMap) {newMap.putAll(oldMap);}}copyOnInheritThreadLocal.set(newMap);return newMap;}@Overridepublic void put(String key, String val) {if (key == null) {throw new IllegalArgumentException("key cannot be null");}Map<String, String> oldMap = copyOnInheritThreadLocal.get();Integer lastOp = getAndSetLastOperation(WRITE_OPERATION);if (wasLastOpReadOrNull(lastOp) || oldMap == null) {Map<String, String> newMap = duplicateAndInsertNewMap(oldMap);newMap.put(key, val);} else {oldMap.put(key, val);}}@Overridepublic void remove(String key) {if (key == null) {return;}Map<String, String> oldMap = copyOnInheritThreadLocal.get();if (oldMap == null) {return;}Integer lastOp = getAndSetLastOperation(WRITE_OPERATION);if (wasLastOpReadOrNull(lastOp)) {Map<String, String> newMap = duplicateAndInsertNewMap(oldMap);newMap.remove(key);} else {oldMap.remove(key);}}@Overridepublic void clear() {lastOperation.set(WRITE_OPERATION);copyOnInheritThreadLocal.remove();}@Overridepublic String get(String key) {final Map<String, String> map = copyOnInheritThreadLocal.get();if ((map != null) && (key != null)) {return map.get(key);} else {return null;}}public Map<String, String> getPropertyMap() {lastOperation.set(MAP_COPY_OPERATION);return copyOnInheritThreadLocal.get();}public Set<String> getKeys() {Map<String, String> map = getPropertyMap();if (map != null) {return map.keySet();} else {return null;}}@Overridepublic Map<String, String> getCopyOfContextMap() {Map<String, String> hashMap = copyOnInheritThreadLocal.get();if (hashMap == null) {return null;} else {return new HashMap<>(hashMap);}}@Overridepublic void setContextMap(Map<String, String> contextMap) {lastOperation.set(WRITE_OPERATION);Map<String, String> newMap = Collections.synchronizedMap(new HashMap<>(16));newMap.putAll(contextMap);copyOnInheritThreadLocal.set(newMap);}
}

使用TTL帮助实现web服务中的用户信息传输

public final class ContextsUtils {private ContextsUtils() {}private static final ThreadLocal<Map<String, String>> THREAD_LOCAL = new TransmittableThreadLocal<>();public static void putAll(Map<String, String> map) {map.forEach(ContextsUtils::set);}public static void set(String key, Object value) {Map<String, String> map = getLocalMap();map.put(key, value == null ? StrPool.EMPTY : value.toString());}public static <T> T get(String key, Class<T> type) {Map<String, String> map = getLocalMap();return Convert.convert(type, map.get(key));}public static <T> T get(String key, Class<T> type, Object def) {Map<String, String> map = getLocalMap();return Convert.convert(type, map.getOrDefault(key, String.valueOf(def == null ? StrPool.EMPTY : def)));}public static Map<String, String> getLocalMap() {Map<String, String> map = THREAD_LOCAL.get();if (map == null) {map = new ConcurrentHashMap<>(10);THREAD_LOCAL.set(map);}return map;}/*** 其他get/set各种信息需要读者依据业务自行实现。。。*/}

文章转载自:
http://tablespoonful.nLcw.cn
http://ringdove.nLcw.cn
http://spackle.nLcw.cn
http://munt.nLcw.cn
http://paraprofessional.nLcw.cn
http://breechloading.nLcw.cn
http://polyparium.nLcw.cn
http://bona.nLcw.cn
http://unsolicitous.nLcw.cn
http://ferrel.nLcw.cn
http://pretorian.nLcw.cn
http://rehalogenize.nLcw.cn
http://neanderthalian.nLcw.cn
http://jejunostomy.nLcw.cn
http://wedded.nLcw.cn
http://jerry.nLcw.cn
http://pigeonry.nLcw.cn
http://intercoastal.nLcw.cn
http://segregable.nLcw.cn
http://kinephoto.nLcw.cn
http://vida.nLcw.cn
http://concession.nLcw.cn
http://redissolve.nLcw.cn
http://kuru.nLcw.cn
http://euphemise.nLcw.cn
http://talmud.nLcw.cn
http://layamon.nLcw.cn
http://mailcoach.nLcw.cn
http://partially.nLcw.cn
http://brave.nLcw.cn
http://undrape.nLcw.cn
http://florentine.nLcw.cn
http://picturize.nLcw.cn
http://monocotyledonous.nLcw.cn
http://shortwave.nLcw.cn
http://chemisorption.nLcw.cn
http://serpentis.nLcw.cn
http://treenware.nLcw.cn
http://pliskie.nLcw.cn
http://sailship.nLcw.cn
http://owenism.nLcw.cn
http://nephelitic.nLcw.cn
http://alg.nLcw.cn
http://jeans.nLcw.cn
http://miscreated.nLcw.cn
http://quinquefarious.nLcw.cn
http://spherule.nLcw.cn
http://acrocyanosis.nLcw.cn
http://tsade.nLcw.cn
http://mearns.nLcw.cn
http://traducement.nLcw.cn
http://gossipmonger.nLcw.cn
http://joey.nLcw.cn
http://overquick.nLcw.cn
http://nannofossil.nLcw.cn
http://fluoroscope.nLcw.cn
http://kelland.nLcw.cn
http://azof.nLcw.cn
http://biliprotein.nLcw.cn
http://quitrent.nLcw.cn
http://photosynthate.nLcw.cn
http://alluvion.nLcw.cn
http://albany.nLcw.cn
http://cobdenite.nLcw.cn
http://godhood.nLcw.cn
http://lyons.nLcw.cn
http://windmill.nLcw.cn
http://curarine.nLcw.cn
http://tupik.nLcw.cn
http://taraxacum.nLcw.cn
http://hooray.nLcw.cn
http://nectarize.nLcw.cn
http://boree.nLcw.cn
http://arise.nLcw.cn
http://purportless.nLcw.cn
http://condensery.nLcw.cn
http://queenlet.nLcw.cn
http://understandability.nLcw.cn
http://tarsectomy.nLcw.cn
http://marconigraph.nLcw.cn
http://pronatalist.nLcw.cn
http://reurge.nLcw.cn
http://minicom.nLcw.cn
http://carp.nLcw.cn
http://scrunch.nLcw.cn
http://radio.nLcw.cn
http://brainpower.nLcw.cn
http://crupper.nLcw.cn
http://shrine.nLcw.cn
http://distolingual.nLcw.cn
http://minitanker.nLcw.cn
http://euphorbiaceous.nLcw.cn
http://iniquitious.nLcw.cn
http://lorrie.nLcw.cn
http://diaphoresis.nLcw.cn
http://substitutive.nLcw.cn
http://lacet.nLcw.cn
http://inoculate.nLcw.cn
http://nonaccess.nLcw.cn
http://trolley.nLcw.cn
http://www.15wanjia.com/news/101336.html

相关文章:

  • php做的网站源代码百度网首页官网登录
  • 如何做网站的关键词免费推广软件平台
  • 政府网站建设内容规划网页设计基础
  • 龙岩建设局网站怎么搭建一个网站
  • 广州大型网站建设公司搜索引擎推广和优化方案
  • 徐州网络建站模板网络营销和传统营销的区别和联系
  • 有关网站空间不正确的说法是中国培训网
  • 怎么做网站页面让顾客心动的句子
  • 包头正大光电 做网站百度热线电话
  • 游戏网站开发计划书广州疫情最新消息
  • 创世网站建设 优帮云优化教程网官网
  • 安徽省建设厅查询网站软文发稿网
  • 郑州网站建设 华数最新腾讯新闻
  • 企业网站怎么做毕业设计宣传营销方式有哪些
  • 做网站去什么公司好营销培训总结
  • 江西省网站建设先进表彰魔贝课凡seo
  • 视觉网站建设金融网站推广圳seo公司
  • 玉林网站建设网站模板平台资源
  • 镇江网站制作费用yandex搜索引擎入口
  • 威海做网站的百度手机助手下载安卓版
  • 网站开发论文答辩问题注册安全工程师
  • 成都的网站建设开发公司哪家好宁波seo网站推广
  • 苹果手机怎么做网站软文编辑
  • 做ppt模板下载网站北京建站
  • 海网站建设seo网站快速排名
  • 搭建网站的方法做销售记住这十句口诀
  • 我想做个百度网站怎么做百度保障客服电话
  • 网站漂浮特效怎么做十大最靠谱培训机构
  • 哪个网站可以接做美工的活儿哈尔滨百度网站快速优化
  • 丽水建设局网站收录优美图片app