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

那些网站可以做外链搜索关键词然后排名怎样提升

那些网站可以做外链,搜索关键词然后排名怎样提升,怎么知道网站用wordpress,长治软件制作平台0、前言 TransmittableThreadLocal,简称 TTL,是阿里巴巴开源的一个Java库,它能够实现ThreadLocal在多线程间的值传递,适用于使用线程池、异步调用等需要线程切换的场景,解决了ThreadLocal在使用父子线程、线程池时不能…

0、前言

  TransmittableThreadLocal,简称 TTL,是阿里巴巴开源的一个Java库,它能够实现ThreadLocal在多线程间的值传递,适用于使用线程池、异步调用等需要线程切换的场景,解决了ThreadLocal在使用父子线程、线程池时不能正确传递值的问题。
核心实现:捕获(capture)- 重放(replay)- 恢复(restore)

  • 捕获:将父线程的 TTL/ThreadLocal 拷贝一份到子线程中存为快照;
    private static class Snapshot {final HashMap<TransmittableThreadLocal<Object>, Object> ttl2Value;final HashMap<ThreadLocal<Object>, Object> threadLocal2Value;}
  • 重放:将快照中的内容存入子线程的 TTL/ThreadLocal 中,并移除不存在快照中的子线程已经存在的 TTL/ThreadLocal;
  • 恢复:清除子线程的 TTL/ThreadLocal。

1、上下文乱象

  背景:为了实现在异步线程中也能正确进行通用字段的填充,引入了 TTL,将原先存储用户上下文信息的 ThreadLocal 换成了 TTL。(注:异步线程通过线程池进行管理)
乱象: 子线程在执行任务的过程中,用户上下文出现了两种状态:run() 执行前后 – 正确信息、run() 执行中 – null,如下图所示。
92f76f1daac51510027eba1a7ca6fe2.png
  代码部分:如下所示。
功能逻辑
4dce615fa1ada1fb99324bad6164d5d.png
线程池装饰器
5a1952f94ac395955a62306e05346ba.png
字段填充
c1759bb945a2cc8c5cc3f7c67d8eb9d.png

2、没有使用 TtlRunnable

  capture,replay,restore 本质是线程任务执行前后的增强方法,这些方法的调用发生于 TtlRunnable 的 run 方法中。

    /*** wrap method {@link Runnable#run()}.*/@Overridepublic void run() {final Object captured = capturedRef.get();if (captured == null || releaseTtlValueReferenceAfterRun && !capturedRef.compareAndSet(captured, null)) {throw new IllegalStateException("TTL value reference is released after run!");}final Object backup = replay(captured);try {runnable.run();} finally {restore(backup);}}

使用方式:

  • 直接调用 TtlRunnable.get(…) 对 Runnable 进行包装增强;
  • 通过 TtlExecutors 工具类获取相应的包装类。

错误例子:
image.png
正确例子:
image.png

3、父子线程引用共享问题

  TTL 默认的上下文复制方式是浅拷贝,这就会造成父子线程中的上下文信息出现共享问题。解决这一问题的方法为:重写 TTL 的 copy 方法,将浅拷贝换成深拷贝。

    /*** Computes the value for this transmittable thread-local variable* as a function of the source thread's value at the time the task* Object is created.* <p>* This method is called from {@link TtlRunnable} or* {@link TtlCallable} when it create, before the task is started.* <p>* This method merely returns reference of its source thread value(the shadow copy),* and should be overridden if a different behavior is desired.** @since 1.0.0*/public T copy(T parentValue) {return parentValue;}

错误例子:

    private final static ThreadLocal<Map<String, Integer>> transmittableThreadLocal = new TransmittableThreadLocal<Map<String, Integer>>() {@Overrideprotected Map<String, Integer> initialValue() {return new HashMap<>();}};private static int i = 0;public static void main(String[] args) {transmittableThreadLocal.get().put(String.format("key-%d", ++i), i);Executor ttlExecutor = TtlExecutors.getTtlExecutor(Executors.newFixedThreadPool(1));CompletableFuture.runAsync(()-> {try {Thread.sleep(3 * 1000);} catch (InterruptedException e) {}System.out.println(StrUtil.format("[{}]子线程:{}", LocalTime.now(), transmittableThreadLocal.get()));}, ttlExecutor);transmittableThreadLocal.get().put(String.format("key-%d", ++i), i);System.out.println(StrUtil.format("[{}]父线程:{}", LocalTime.now(), transmittableThreadLocal.get()));transmittableThreadLocal.remove();}

image.png
正确例子:

   private final static ThreadLocal<Map<String, Integer>> transmittableThreadLocal = new TransmittableThreadLocal<Map<String, Integer>>() {@Overrideprotected Map<String, Integer> initialValue() {return new HashMap<>();}@Overridepublic Map<String, Integer> copy(Map<String, Integer> parentValue) {return parentValue != null ? new HashMap<>(parentValue) : null;}};private static int i = 0;public static void main(String[] args) {transmittableThreadLocal.get().put(StrUtil.format("key-{}", ++i), i);Executor ttlExecutor = TtlExecutors.getTtlExecutor(Executors.newFixedThreadPool(1));CompletableFuture.runAsync(()-> {try {Thread.sleep(3 * 1000);} catch (InterruptedException e) {}System.out.println(StrUtil.format("[{}]子线程:{}", LocalTime.now(), transmittableThreadLocal.get()));}, ttlExecutor);transmittableThreadLocal.get().put(String.format("key-%d", ++i), i);System.out.println(StrUtil.format("[{}]父线程:{}", LocalTime.now(), transmittableThreadLocal.get()));transmittableThreadLocal.remove();}

image.png


拓展:捕获、重放期间的线程切换和 ThreadLocal 变化。
捕获:
image.png
image.png
重放:

  • 备份

image.png
image.png

  • 重新设置

image.png
image.png


http://www.ritt.cn/news/17365.html

相关文章:

  • 代理记账网站怎么做怎么制作自己的个人网站
  • 深圳网站建设联雅网络云浮seo
  • 论吉林省网站职能建设网络公司经营范围
  • 纯静态网站怎么做cdn建站平台哪个好
  • 上海城隍庙旅游区图片优化网站
  • php网站调试环境搭建国内做网站的公司
  • 沈阳网站建设兼职网站seo啥意思
  • 网站可以给pdf做笔记百度指数查询排行榜
  • 电子商务网站建设复习题怎样制作网站
  • 外贸网站产品分析培训学校怎么招生
  • 盘锦企业网站建设友情下载网站
  • 网站建设制作文字教程搜索引擎营销概念
  • 通过网站seo操作超能搜索引擎系统网站
  • 长沙做产检玛丽亚m网站百度推广账号
  • 湖南省建设厅安许审核公布网站全渠道营销案例
  • 做网站公司合同网络营销的主要方式和技巧
  • 深圳趣网站建设中国互联网协会
  • 福安网站开发怎么自己做网页
  • 邯郸专业做网站报价免费发布信息的平台有哪些
  • 网站频道策划安全又舒适的避孕方法有哪些
  • 一个人做网站 没有人写文章怎么办菏泽seo
  • 网站都是怎么做的seo优化需要做什么
  • 苏州网站制作真正免费建站网站
  • 网站源码什么意思软文营销的本质
  • 济南做网站公司xywlcn视频营销成功的案例
  • 有没有专业做效果图的网站高端网站定制
  • 怎么用wordpress做网站alexa排名查询统计
  • 网站添加可信任站点怎么做谷歌seo视频教程
  • 石湾网站建设东莞seo网络推广专
  • 小程序开店流程安徽seo推广公司