日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

何为TransmittableThreadLocal

發(fā)布時(shí)間:2024/1/17 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 何为TransmittableThreadLocal 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、示例

?線程池內(nèi)的線程并沒有父子關(guān)系,所以不適合InheritableThreadLocal的使用場景

public class ThreadPoolInheritableThreadLocalDemo {// static ThreadLocal<String> threadLocal = new InheritableThreadLocal<>(); // static ExecutorService pool = Executors.newFixedThreadPool(2);static TransmittableThreadLocal<String> threadLocal = new TransmittableThreadLocal<>();static ExecutorService pool = TtlExecutors.getTtlExecutorService(Executors.newFixedThreadPool(3));public static void main(String[] args) {for(int i=0;i<100;i++) {int j = i;pool.execute(new Thread(new Runnable() {@Overridepublic void run() {ThreadPoolInheritableThreadLocalDemo.threadLocal.set("superWorld"+j);ThreadPoolInheritableThreadLocalDemo.pool.execute(new Runnable() {@Overridepublic void run() {System.out.println(Thread.currentThread().getName() +" : " +ThreadPoolInheritableThreadLocalDemo.threadLocal.get());}}); }}));}}}

?

?

二、TransmittableThreadLocal實(shí)現(xiàn)分析

?

讀取線程間傳遞的ThreadLocal 值比較麻煩,ThreadLocal 和 InheritableThreadLocal 都沒有開放內(nèi)部的 ThreadLocalMap,不能直接讀取。

所以要么自己完全實(shí)現(xiàn)一套 ThreadLocalMap 機(jī)制(如 Netty 的 FastThreadLocal),要么就是自己實(shí)現(xiàn) ThreadLocal 的子類,在每次調(diào)用 ThreadLocal

的 set/get/remove 等接口的時(shí)候,為 Thread 記錄到底綁定了哪些需要發(fā)生線程間傳遞的 ThreadLocal 對象。

/***實(shí)際存儲(chǔ)值的工作還是父類ThreadLocal完成*TransmittableThreadLocal 只是記錄了哪些線程使用了TransmittableThreadLocal對象*/@Overridepublic final void set(T value) {super.set(value);if (null == value) { // may set null to remove value removeValue();} else {addValue();}}/***holder 只是為了記錄使用了哪些 TransmittableThreadLocal 對象*在構(gòu)造TtlRunnable/TtlCallable 的時(shí)候, 通過holder取得對應(yīng)的TransmittableThreadLocal
   *InheritableThreadLocal的默認(rèn)值是WeakHashMap
*/private static InheritableThreadLocal<Map<TransmittableThreadLocal<?>, ?>> holder =new InheritableThreadLocal<Map<TransmittableThreadLocal<?>, ?>>() {@Overrideprotected Map<TransmittableThreadLocal<?>, ?> initialValue() {return new WeakHashMap<TransmittableThreadLocal<?>, Object>();}@Overrideprotected Map<TransmittableThreadLocal<?>, ?> childValue(Map<TransmittableThreadLocal<?>, ?> parentValue) {return new WeakHashMap<TransmittableThreadLocal<?>, Object>(parentValue);}};private void addValue() {if (!holder.get().containsKey(this)) {holder.get().put(this, null); // WeakHashMap supports null value. }}private void removeValue() {holder.get().remove(this);}

?

調(diào)用ThreadPoolInheritableThreadLocalDemo.threadLocal.set("superWorld"+j)時(shí),?

holder.get().containskey(this) 為false

?

?

2.?TtlRunnable

?構(gòu)造TtlRunable時(shí),設(shè)置線程對應(yīng)的Map<TransmittableThreadLocal<?>, Object>>

private TtlRunnable(Runnable runnable, boolean releaseTtlValueReferenceAfterRun) {
     //
this.copiedRef = new AtomicReference<Map<TransmittableThreadLocal<?>, Object>>(TransmittableThreadLocal.copy());this.runnable = runnable;this.releaseTtlValueReferenceAfterRun = releaseTtlValueReferenceAfterRun;}

?

TransmittableThreadLocal.copy static Map<TransmittableThreadLocal<?>, Object> copy() {Map<TransmittableThreadLocal<?>, Object> copy = new HashMap<TransmittableThreadLocal<?>, Object>();for (TransmittableThreadLocal<?> threadLocal : holder.get().keySet()) {copy.put(threadLocal, threadLocal.copyValue());}return copy;}

?

?

3.運(yùn)行時(shí),備份和恢復(fù)Map<TransmittableThreadLocal<?>, Object>

TtlRunnable#run

@Overridepublic void run() {Map<TransmittableThreadLocal<?>, Object> copied = copiedRef.get();if (copied == null || releaseTtlValueReferenceAfterRun && !copiedRef.compareAndSet(copied, null)) {throw new IllegalStateException("TTL value reference is released after run!");}Map<TransmittableThreadLocal<?>, Object> backup = TransmittableThreadLocal.backupAndSetToCopied(copied);try {runnable.run();} finally {TransmittableThreadLocal.restoreBackup(backup);}}

?

?

?

?

參考:

transmittableThreadLocal

總結(jié)

以上是生活随笔為你收集整理的何为TransmittableThreadLocal的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。