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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

TransmittableThreadLocal 解决 线程池线程复用 无法复制 InheritableThreadLocal 的问题

發布時間:2024/1/17 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 TransmittableThreadLocal 解决 线程池线程复用 无法复制 InheritableThreadLocal 的问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ThreadLoacl,InheritableThreadLocal,原理,以及配合線程池使用的一些坑

?

TransmittableThreadLocal?原理

?

之前為了能讓InheritableThreadLocal?正確傳遞,不得不每次

ExecutorService executor = Executors.newFixedThreadPool(>=[任務線程數]); 或者直接new Thread.這樣不僅引起性能損耗,并且如果并發上來了,會造成不必要的上下文切換.還必須用信號量做并發控制. 偶然發現 阿里開源 TransmittableThreadLocal?可以解決此問題. 以下來實驗一下

?

/*** User: laizhenwei* Date: 2018-04-12 Time: 10:07* Description:*/ public class Ttl {static ExecutorService executorService = Executors.newFixedThreadPool(1);public static void main(String[] args) {//子線程每次new 所以會復制線程的InheritableThreadLocal,結果正確 // withoutThreadPool(10);//因線程池復用線程,不會每次new 所以不會更新父線程InheritableThreadLocal 的值,導致結果錯誤withThreadPool(10);}public static void withoutThreadPool(int c){for(int i=0;i<c;i++){Integer var1 = (int)(Math.random()*100);Integer var2 = (int)(Math.random()*100);MyContextHolder.set(var1);threadRun(var1,var2);}}public static void withThreadPool(int c){for(int i=0;i<c;i++){Integer var1 = (int)(Math.random()*100);Integer var2 = (int)(Math.random()*100);MyContextHolder.set(var1);threadPoolExecute(var1,var2);}}public static void threadRun(Integer var1,Integer var2){new Thread(()->assert1(var1,var2)).start();}public static void threadPoolExecute(Integer var1,Integer var2){executorService.execute(()->assert1(var1,var2));}public static void assert1(Integer var1,Integer var2){System.out.println(MyContextHolder.get()*var2==var1*var2);}public static class MyContextHolder{private static ThreadLocal<Integer> stringThreadLocal = new InheritableThreadLocal<>();public static void set(Integer data) {stringThreadLocal.set(data);}public static Integer get() {return stringThreadLocal.get();}}}

withoutThreadPool(10)輸出結果

withThreadPool(10); 輸出結果

?

解決方式

pom引入

<!-- https://mvnrepository.com/artifact/com.alibaba/transmittable-thread-local --><dependency><groupId>com.alibaba</groupId><artifactId>transmittable-thread-local</artifactId><version>2.2.0</version></dependency>

修改MyContextHolder

public static class MyContextHolder{private static ThreadLocal<Integer> stringThreadLocal = new TransmittableThreadLocal<>();// private static ThreadLocal<Integer> stringThreadLocal = new InheritableThreadLocal<>();public static void set(Integer data) {stringThreadLocal.set(data);}public static Integer get() {return stringThreadLocal.get();}}

修改threadPoolExecute

public static void threadPoolExecute(Integer var1,Integer var2){//使用TransmittableThreadLocal 解決executorService.execute(TtlRunnable.get(()->assert1(var1,var2)) ); // executorService.execute(()->assert1(var1,var2));}

運行?withThreadPool(10);?結果

?

總結

以上是生活随笔為你收集整理的TransmittableThreadLocal 解决 线程池线程复用 无法复制 InheritableThreadLocal 的问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。