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

坪山商城网站建设哪家公司靠谱口碑营销的形式

坪山商城网站建设哪家公司靠谱,口碑营销的形式,机械设备网站建设,免费网站建设模板前言 ThreadLocal在上下文的数据传输上非常的方便和简洁。工业实践中,比较常用的有三个,ThreadLocal、InheritableThreadLocal、TransmittableThreadLocal,那么他们三个之间有什么区别呢? 常见的三种ThreadLocal比较 ThreadLoc…

前言

ThreadLocal在上下文的数据传输上非常的方便和简洁。工业实践中,比较常用的有三个,ThreadLocal、InheritableThreadLocal、TransmittableThreadLocal,那么他们三个之间有什么区别呢?

常见的三种ThreadLocal比较

ThreadLocalInheritableThreadLocalTransmittableThreadLocal
来源jdkjdk阿里开源
单线程数据传输支持支持支持
new线程数据传输不支持支持支持
线程池数据传输不支持部分支持【简单场景】支持

针对线程池的数据传输,InheritableThreadLocal仅仅能在一些简单场景下做到,下面就用一个案例来说明

先看下线程工厂的定义

package com.tml.mouseDemo.core.threadLocalDemo;import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;public class SimpleThreadFactory implements ThreadFactory {private static AtomicInteger atomicInteger = new AtomicInteger(1);@Overridepublic Thread newThread(Runnable r) {Thread t = new Thread(r);t.setName("tml-"+atomicInteger.getAndIncrement());return t;}
}

 InheritableThreadLocal部分支持的案例

package com.tml.mouseDemo.core.threadLocalDemo;import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class ThreadLocalDemo1 {private static ThreadLocal<String> threadLocal = new InheritableThreadLocal<>();private static ExecutorService service = Executors.newFixedThreadPool(2, new SimpleThreadFactory());public static void main(String[] args) throws InterruptedException {threadLocal.set("hello main");for (int i = 0; i < 2; i++) {service.execute(() -> {String s = threadLocal.get();ThreadUtils.printLog("get data " + s);});}//修改threadLocal中的值threadLocal.set("hello world");Thread.sleep(2000);for (int i = 0; i < 2; i++) {service.execute(() -> {String s = threadLocal.get();ThreadUtils.printLog("get data " + s);});}ThreadUtils.printLog("get data " + threadLocal.get());service.shutdown();}
}

运行结果如下 

2025-01-10 19:41:50 | INFO  | tml-1 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello main
2025-01-10 19:41:50 | INFO  | tml-2 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello main
2025-01-10 19:41:52 | INFO  | tml-2 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello main
2025-01-10 19:41:52 | INFO  | main | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello world
2025-01-10 19:41:52 | INFO  | tml-1 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello main

从运行结果来看,前面的两次循环提交的任务,在子线程中确实是能正常的获取主线程设置的变量,即hello main,但是紧接着,我修改了主线程上绑定的变量为hello world,然后继续循环两次提交两个任务,这个时候子线程中获取的线程变量依然是hello main,这明显是与是期望不一致的。

从这个层面来讲,InheritableThreadLocal确实在线程池的层面支持不够友好,可以说仅支持部分简单场景。

根本原因就死线程池的池化机制,从上面的运行日志上也可以看出,提交了4个任务执行的线程依然是两个。线程池中的线程是复用的,InheritableThreadLocal是在创建子线程的时候,会将主线程上绑定的数据拷贝过来,如果我不创建新的线程,但是主线程上绑定的数据改变了呢?那我依然还是读取到之前拷贝的数据。

这个就是InheritableThreadLocal的短板。针对这个问题,阿里开源的TransmittableThreadLocal就能顺利丝滑的解决这个问题。

TransmittableThreadLocal实践

maven依赖

<dependency><groupId>com.alibaba</groupId><artifactId>transmittable-thread-local</artifactId><version>2.12.4</version>
</dependency>

装饰Runnable任务

直接看代码

package com.tml.mouseDemo.core.threadLocalDemo;import com.alibaba.ttl.TransmittableThreadLocal;
import com.alibaba.ttl.TtlRunnable;import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class ThreadLocalDemo1 {private static ThreadLocal<String> threadLocal = new TransmittableThreadLocal<>();private static ExecutorService service = Executors.newFixedThreadPool(2, new SimpleThreadFactory());public static void main(String[] args) throws InterruptedException {threadLocal.set("hello main");for (int i = 0; i < 2; i++) {service.execute(TtlRunnable.get(() -> {String s = threadLocal.get();ThreadUtils.printLog("get data " + s);}));}//修改threadLocal中的值threadLocal.set("hello world");Thread.sleep(2000);for (int i = 0; i < 2; i++) {service.execute(TtlRunnable.get(() -> {String s = threadLocal.get();ThreadUtils.printLog("get data " + s);}));}ThreadUtils.printLog("get data " + threadLocal.get());service.shutdown();}
}

运行结果如下:

 2025-01-10 19:57:03 | INFO  | tml-2 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello main
2025-01-10 19:57:03 | INFO  | tml-1 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello main
2025-01-10 19:57:05 | INFO  | tml-1 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello world
2025-01-10 19:57:05 | INFO  | main | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello world
2025-01-10 19:57:05 | INFO  | tml-2 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello world

与第一个案例的差异点在于,使用了

public static TtlRunnable get(@Nullable Runnable runnable) {return get(runnable, false, false);
}

 来增强了了Runnable任务,执行的结果也是符合预期。

装饰线程池

直接看代码

package com.tml.mouseDemo.core.threadLocalDemo;import com.alibaba.ttl.TransmittableThreadLocal;
import com.alibaba.ttl.TtlRunnable;
import com.alibaba.ttl.threadpool.TtlExecutors;import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class ThreadLocalDemo1 {private static ThreadLocal<String> threadLocal = new TransmittableThreadLocal<>();private static ExecutorService service = Executors.newFixedThreadPool(2, new SimpleThreadFactory());private static ExecutorService ttlExecutorService = TtlExecutors.getTtlExecutorService(service);public static void main(String[] args) throws InterruptedException {threadLocal.set("hello main");for (int i = 0; i < 2; i++) {ttlExecutorService.execute(() -> {String s = threadLocal.get();ThreadUtils.printLog("get data " + s);});}//修改threadLocal中的值threadLocal.set("hello world");Thread.sleep(2000);for (int i = 0; i < 2; i++) {ttlExecutorService.execute(() -> {String s = threadLocal.get();ThreadUtils.printLog("get data " + s);});}ThreadUtils.printLog("get data " + threadLocal.get());service.shutdown();}
}

 运行结果如下

2025-01-10 20:05:05 | INFO  | tml-2 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello main
2025-01-10 20:05:05 | INFO  | tml-1 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello main
2025-01-10 20:05:07 | INFO  | tml-2 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello world
2025-01-10 20:05:07 | INFO  | main | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello world
2025-01-10 20:05:07 | INFO  | tml-1 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello world

与上一个案例的差异点在于,这里没有包装Runnable任务,而是包装了线程池,使用了

public static ExecutorService getTtlExecutorService(@Nullable ExecutorService executorService) {if (TtlAgent.isTtlAgentLoaded() || executorService == null || executorService instanceof TtlEnhanced) {return executorService;}return new ExecutorServiceTtlWrapper(executorService, true);
}

包装了ExecutorService,执行结果也是符合预期

使用java Agent无侵入增强线程池

直接看代码

package com.tml.mouseDemo.core.threadLocalDemo;import com.alibaba.ttl.TransmittableThreadLocal;import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class ThreadLocalDemo1 {private static ThreadLocal<String> threadLocal = new TransmittableThreadLocal<>();private static ExecutorService service = Executors.newFixedThreadPool(2, new SimpleThreadFactory());public static void main(String[] args) throws InterruptedException {threadLocal.set("hello main");for (int i = 0; i < 2; i++) {service.execute(() -> {String s = threadLocal.get();ThreadUtils.printLog("get data " + s);});}//修改threadLocal中的值threadLocal.set("hello world");Thread.sleep(2000);for (int i = 0; i < 2; i++) {service.execute(() -> {String s = threadLocal.get();ThreadUtils.printLog("get data " + s);});}ThreadUtils.printLog("get data " + threadLocal.get());service.shutdown();}
}

 项目运行的时候,需要添加额外的jvm启动参数,如下

运行结果如下,也是符合预期

 2025-01-10 20:11:59 | INFO  | tml-2 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello main
2025-01-10 20:11:59 | INFO  | tml-1 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello main
2025-01-10 20:12:01 | INFO  | tml-2 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello world
2025-01-10 20:12:01 | INFO  | main | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello world
2025-01-10 20:12:01 | INFO  | tml-1 | com.tml.mouseDemo.core.threadLocalDemo.ThreadUtils | get data hello world

总结

阿里巴巴的TransmittableThreadLocal是继承自InheritableThreadLocal,对他的功能进行了增强,增强的点也主要是在线程池的支持上。

通过上面的三个案例,可以看到TransmittableThreadLocal是非常灵活的,大家可以根据自己的需要,选择对应的方式来实现。

TransmittableThreadLocal的官网GitCode - 全球开发者的开源社区,开源代码托管平台


文章转载自:
http://dinncoradioautograph.knnc.cn
http://dinncokeratolytic.knnc.cn
http://dinncoqinghai.knnc.cn
http://dinncocheapen.knnc.cn
http://dinncowhitecap.knnc.cn
http://dinncotelelectric.knnc.cn
http://dinncocyanic.knnc.cn
http://dinncowashaway.knnc.cn
http://dinncobacchae.knnc.cn
http://dinncointerfinger.knnc.cn
http://dinncocharry.knnc.cn
http://dinncoadhocery.knnc.cn
http://dinncosideswipe.knnc.cn
http://dinncocanada.knnc.cn
http://dinncononwhite.knnc.cn
http://dinncowhereat.knnc.cn
http://dinncowolfess.knnc.cn
http://dinncoflunk.knnc.cn
http://dinncoa.knnc.cn
http://dinncounderling.knnc.cn
http://dinncorototill.knnc.cn
http://dinncohaematemesis.knnc.cn
http://dinncoteosinte.knnc.cn
http://dinncoaccouche.knnc.cn
http://dinncopseudoallele.knnc.cn
http://dinncopavid.knnc.cn
http://dinncograssiness.knnc.cn
http://dinncoconsultive.knnc.cn
http://dinncopekalongan.knnc.cn
http://dinncobout.knnc.cn
http://dinncoprepreg.knnc.cn
http://dinncohyperosmolality.knnc.cn
http://dinnconostalgic.knnc.cn
http://dinncohelioscope.knnc.cn
http://dinncoprolongate.knnc.cn
http://dinncocongeal.knnc.cn
http://dinncomaris.knnc.cn
http://dinncoimpetigo.knnc.cn
http://dinncoprogram.knnc.cn
http://dinncotubuliflorous.knnc.cn
http://dinncogunpoint.knnc.cn
http://dinncopiperidine.knnc.cn
http://dinncorabbin.knnc.cn
http://dinncogandhist.knnc.cn
http://dinncochirp.knnc.cn
http://dinncorecelebration.knnc.cn
http://dinncohound.knnc.cn
http://dinncosetup.knnc.cn
http://dinncononperson.knnc.cn
http://dinncotelepathise.knnc.cn
http://dinncostipel.knnc.cn
http://dinncochicagoan.knnc.cn
http://dinncodroningly.knnc.cn
http://dinncooccasionalism.knnc.cn
http://dinncohaematocrit.knnc.cn
http://dinncounesthetic.knnc.cn
http://dinncoruche.knnc.cn
http://dinncounpowered.knnc.cn
http://dinncobluestem.knnc.cn
http://dinncothousandfold.knnc.cn
http://dinncosellout.knnc.cn
http://dinncoquellenforschung.knnc.cn
http://dinncosupposition.knnc.cn
http://dinncocanonist.knnc.cn
http://dinncocloudburst.knnc.cn
http://dinncoloath.knnc.cn
http://dinncopathetic.knnc.cn
http://dinncorabbinist.knnc.cn
http://dinncocactaceous.knnc.cn
http://dinncothermoform.knnc.cn
http://dinncodahabiah.knnc.cn
http://dinncosanty.knnc.cn
http://dinncorusa.knnc.cn
http://dinncomethanation.knnc.cn
http://dinncophotophase.knnc.cn
http://dinncothc.knnc.cn
http://dinncotenebrious.knnc.cn
http://dinncountypable.knnc.cn
http://dinncohorsenapping.knnc.cn
http://dinncodisobliging.knnc.cn
http://dinncowaist.knnc.cn
http://dinncodimwitted.knnc.cn
http://dinncomendable.knnc.cn
http://dinncojuryman.knnc.cn
http://dinncosoliped.knnc.cn
http://dinncoergonomist.knnc.cn
http://dinncogrumpily.knnc.cn
http://dinncominiaturist.knnc.cn
http://dinncoiorm.knnc.cn
http://dinncodeponent.knnc.cn
http://dinncoexultation.knnc.cn
http://dinncodung.knnc.cn
http://dinncominicoy.knnc.cn
http://dinncoantihydrogen.knnc.cn
http://dinncoderegister.knnc.cn
http://dinncospinthariscope.knnc.cn
http://dinncoeverywoman.knnc.cn
http://dinncopediculous.knnc.cn
http://dinncoprocreator.knnc.cn
http://dinncomashie.knnc.cn
http://www.dinnco.com/news/155816.html

相关文章:

  • 网站建设 深圳怎么根据视频链接找到网址
  • 宝安区做网站网站技术解决方案
  • 网站开发界面图标设计吴江seo网站优化软件
  • 鞍山网站建设公司网站子域名查询
  • 隆尧企业做网站优秀网站设计网站
  • 做毕业设计的参考文献网站友点企业网站管理系统
  • 凡客网站做SEO能被收录吗标题关键词优化报价
  • 旅游网站排名全球信息流优化师培训机构
  • 广东深圳手机号码南昌seo网站排名
  • 杭州知名网站建设百度开户要多少钱
  • 义乌网站建设联系方式google 优化推广
  • 彩票走势图网站是用什么程序做的微商软文范例
  • bing 网站管理员网络营销策略案例分析
  • 网站建设客户需求分析表今日国内新闻头条
  • 自己如何做公司网站视频郑州seo外包顾问
  • 海外模板网站有哪些网店怎么运营和推广
  • 深圳海外网站建设网页设计论文
  • 衢州网络公司做网站站长工具seo推广秒收录
  • 梧州网站建设seo模拟点击软件源码
  • wordpress post.phpseoul是什么国家
  • 营销图片素材360优化大师软件
  • 安徽汽车网网站建设百度文库个人登录
  • 西安做网站公司页面优化算法
  • 做网站一般图片的比例搜索引擎优化的方法和技巧
  • 购物网站怎么做推广seo网站排名厂商定制
  • 企业服务官网模板seo超级外链工具
  • 重庆日报seo优化关键词放多少合适
  • 网站建设一二级目录宁波网站推广方式怎么样
  • 做网站要多少带宽沧州网站优化公司
  • ens域名注册网站seo方案策划书