做影视网站挣钱吗上海哪里有网站建设

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

做影视网站挣钱吗,上海哪里有网站建设,wordpress插件 flyzoo,介绍什么是网页设计【多线程开发 2】从代码到实战TransmittableThreadLocal 本文将从以下几个点讲解TransmittableThreadLocal(为了方便写以下简称ttl)#xff1a; 前身 是什么#xff1f; 可以用来做什么#xff1f; 源码原理 实战
前身 ThreadLocal 要了解ttl就要先了解Java自带的类…【多线程开发 2】从代码到实战TransmittableThreadLocal 本文将从以下几个点讲解TransmittableThreadLocal(为了方便写以下简称ttl) 前身 是什么 可以用来做什么 源码原理 实战
前身 ThreadLocal 要了解ttl就要先了解Java自带的类ThreadLocalthreadlocal是作为当前线程中属性ThreadLocalMap集合中的某一个Entry的key值Entrythreadlocl,value虽然不同的线程之间threadlocal这个key值是一样但是不同的线程所拥有的ThreadLocalMap是独一无二的用于存储一些线程不安全的公共变量通过“给每一个线程一个线程不安全的变量的拷贝”来达到线程安全的目的就不会出现变量多个线程之间共享的问题。 ThreadLocal 变量通常被private static修饰。当一个线程结束时它所使用的所有 ThreadLocal 相对的实例副本都可被回收。 InheritableThreadLocal 使用ThreadLocal可以解决线程安全问题但是也有一定的局限性比如无法在父子线程之间传递信息因此InheritableThreadLocal就是JDK为了解决这个问题而创建的 TTL是什么可以做什么 在现在开发的情况下肯定是需要复用线程的如果说InheritableThreadLocal在生成子进程的时候会做信息传递但是在使用线程池或者其他需要复用线程的地方由于会不产生新的Thread而是直接使用空闲的已建成的Thread所以的话InheritableThreadLocal有时候会不能用此时可以通过使用TTL解决对应问题。 读取线程间传递的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 ThreadLocalMapString, String copyOnInheritThreadLocal new TransmittableThreadLocal();private final ThreadLocalInteger 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 MapString, String duplicateAndInsertNewMap(MapString, String oldMap) {MapString, String newMap Collections.synchronizedMap(new HashMap(16));if (oldMap ! null) {// we dont 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);}MapString, String oldMap copyOnInheritThreadLocal.get();Integer lastOp getAndSetLastOperation(WRITE_OPERATION);if (wasLastOpReadOrNull(lastOp) || oldMap null) {MapString, String newMap duplicateAndInsertNewMap(oldMap);newMap.put(key, val);} else {oldMap.put(key, val);}}Overridepublic void remove(String key) {if (key null) {return;}MapString, String oldMap copyOnInheritThreadLocal.get();if (oldMap null) {return;}Integer lastOp getAndSetLastOperation(WRITE_OPERATION);if (wasLastOpReadOrNull(lastOp)) {MapString, 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 MapString, String map copyOnInheritThreadLocal.get();if ((map ! null) (key ! null)) {return map.get(key);} else {return null;}}public MapString, String getPropertyMap() {lastOperation.set(MAP_COPY_OPERATION);return copyOnInheritThreadLocal.get();}public SetString getKeys() {MapString, String map getPropertyMap();if (map ! null) {return map.keySet();} else {return null;}}Overridepublic MapString, String getCopyOfContextMap() {MapString, String hashMap copyOnInheritThreadLocal.get();if (hashMap null) {return null;} else {return new HashMap(hashMap);}}Overridepublic void setContextMap(MapString, String contextMap) {lastOperation.set(WRITE_OPERATION);MapString, String newMap Collections.synchronizedMap(new HashMap(16));newMap.putAll(contextMap);copyOnInheritThreadLocal.set(newMap);} }使用TTL帮助实现web服务中的用户信息传输 public final class ContextsUtils {private ContextsUtils() {}private static final ThreadLocalMapString, String THREAD_LOCAL new TransmittableThreadLocal();public static void putAll(MapString, String map) {map.forEach(ContextsUtils::set);}public static void set(String key, Object value) {MapString, String map getLocalMap();map.put(key, value null ? StrPool.EMPTY : value.toString());}public static T T get(String key, ClassT type) {MapString, String map getLocalMap();return Convert.convert(type, map.get(key));}public static T T get(String key, ClassT type, Object def) {MapString, String map getLocalMap();return Convert.convert(type, map.getOrDefault(key, String.valueOf(def null ? StrPool.EMPTY : def)));}public static MapString, String getLocalMap() {MapString, String map THREAD_LOCAL.get();if (map null) {map new ConcurrentHashMap(10);THREAD_LOCAL.set(map);}return map;}/*** 其他get/set各种信息需要读者依据业务自行实现。。。*/}