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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

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

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

?

TransmittableThreadLocal?原理

?

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

ExecutorService executor = Executors.newFixedThreadPool(>=[任務(wù)線程數(shù)]);
或者直接new Thread.

這樣不僅引起性能損耗,并且如果并發(fā)上來了,會(huì)造成不必要的上下文切換.還必須用信號(hào)量做并發(fā)控制.
偶然發(fā)現(xiàn) 阿里開源 TransmittableThreadLocal?可以解決此問題.
以下來實(shí)驗(yàn)一下

?

/*** 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 所以會(huì)復(fù)制線程的InheritableThreadLocal,結(jié)果正確 // withoutThreadPool(10);//因線程池復(fù)用線程,不會(huì)每次new 所以不會(huì)更新父線程InheritableThreadLocal 的值,導(dǎo)致結(jié)果錯(cuò)誤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)輸出結(jié)果

withThreadPool(10); 輸出結(jié)果

?

解決方式

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));}

運(yùn)行?withThreadPool(10);?結(jié)果

?


總結(jié)

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

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