首页 > 其他分享 >TransmittableThreadLocal

TransmittableThreadLocal

时间:2024-11-19 16:21:14浏览次数:1  
标签:name get System TransmittableThreadLocal println local out

地址:https://zhuanlan.zhihu.com/p/540626625

pom.xml

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

ThreadDemo.java
package com.demo.cloud.test.thread;
import com.alibaba.ttl.TransmittableThreadLocal;
import com.alibaba.ttl.threadpool.TtlExecutors;import java.util.concurrent.*;

/**
 * @Author: zcjie
 * @create: 2024-11-18 14:55
 * @Description:
 * @Version: 1.0
 */
public class ThreadDemo {

    private static Executor executorOne = new ThreadPoolExecutor(1,1,1, TimeUnit.MINUTES,new ArrayBlockingQueue<>(1));
    private static Executor executorMany = new ThreadPoolExecutor(5,10,1, TimeUnit.MINUTES,new ArrayBlockingQueue<>(10));
    public static void main(String[] args) throws InterruptedException {
        transmittableThreadLocalMany07();
//        transmittableThreadLocalOne06();
//        transmittableThreadLocalOne05();
//        inheritableThreadLocalMany04();
//        inheritableThreadLocalOne03();
//        threadLocalMany02();
//        threadLocalOne01();


    }


    /**
     * TransmittableThreadLocal 现象:
     * 主线程设置1:1
     * pool-1-thread-1子 打印1: 1
     * 主线程设置2:2
     * pool-1-thread-1子 打印2: 2
     *
     * */
    private static void transmittableThreadLocalMany07() {
        executorMany= TtlExecutors.getTtlExecutor(executorMany);
        ThreadLocal local = new TransmittableThreadLocal();

        local.set(1);
        System.out.println("主线程设置1:"+local.get());

        executorMany.execute(()-> {
            String name = Thread.currentThread().getName();
            System.out.println(name+"子 打印1: "+local.get());
        });

        try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {throw new RuntimeException(e);}


        local.set(2);
        System.out.println("主线程设置2:"+local.get());

        executorMany.execute(()-> {
            String name = Thread.currentThread().getName();
            System.out.println(name+"子 打印2:"+local.get());
        });

    }


    /**
     * TransmittableThreadLocal 现象:
     * 主线程设置1:1
     * pool-1-thread-1子 打印1: 1
     * 主线程设置2:2
     * pool-1-thread-1子 打印2: null
     *
     * */
    private static void transmittableThreadLocalOne06() {
        executorOne= TtlExecutors.getTtlExecutor(executorOne);
        ThreadLocal local = new TransmittableThreadLocal();

        local.set(1);
        System.out.println("主线程设置1:"+local.get());

        executorOne.execute(()-> {
            String name = Thread.currentThread().getName();
            System.out.println(name+"子 打印1: "+local.get());
        });

        try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {throw new RuntimeException(e);}


        local.set(2);
        System.out.println("主线程设置2:"+local.get());

        local.remove();

        executorOne.execute(()-> {
            String name = Thread.currentThread().getName();
            System.out.println(name+"子 打印2:"+local.get());
        });

    }

    /**
     * TransmittableThreadLocal 现象:
     * 主线程设置1:1
     * pool-1-thread-1子 打印1: 1
     * 主线程设置2:2
     * pool-1-thread-1子 打印2: 2
     *
     * */
    private static void transmittableThreadLocalOne05() {
        executorOne= TtlExecutors.getTtlExecutor(executorOne);
        ThreadLocal local = new TransmittableThreadLocal();

        local.set(1);
        System.out.println("主线程设置1:"+local.get());

        executorOne.execute(()-> {
            String name = Thread.currentThread().getName();
            System.out.println(name+"子 打印1: "+local.get());
        });

        try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {throw new RuntimeException(e);}


        local.set(2);
        System.out.println("主线程设置2:"+local.get());

        executorOne.execute(()-> {
            String name = Thread.currentThread().getName();
            System.out.println(name+"子 打印2:"+local.get());
        });

    }

    /**
     * InheritableThreadLocal 现象:
     * 主线程设置1:1
     * pool-2-thread-1打印1:1
     * 主线程设置2:2
     * pool-2-thread-2打印3:2
     *
     * */
    private static void inheritableThreadLocalMany04() {
        ThreadLocal local = new InheritableThreadLocal();

        local.set(1);
        System.out.println("主线程设置1:"+local.get());

        executorMany.execute(()-> {
            String name = Thread.currentThread().getName();
            System.out.println(name+"打印1:"+local.get());
        });

        try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {throw new RuntimeException(e);}

        local.set(2);
        System.out.println("主线程设置2:"+local.get());

        executorMany.execute(()-> {
            String name = Thread.currentThread().getName();
            System.out.println(name+"打印3:"+local.get());
        });

    }


    /**
     * InheritableThreadLocal 现象:
     * 主线程设置1:1
     * pool-1-thread-1打印1:1
     * 主线程设置2:2
     * pool-1-thread-1打印3:1
     *
     * 同一线程:
     *  主线程修改内容后子线程获取的还是原来的值
     * */
    private static void inheritableThreadLocalOne03() {
        ThreadLocal local = new InheritableThreadLocal();

        local.set(1);
        System.out.println("主线程设置1:"+local.get());

        executorOne.execute(()-> {
            String name = Thread.currentThread().getName();
            System.out.println(name+"打印1:"+local.get());
        });

        try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {throw new RuntimeException(e);}

        local.set(2);
        System.out.println("主线程设置2:"+local.get());

        executorOne.execute(()-> {
            String name = Thread.currentThread().getName();
            System.out.println(name+"打印3:"+local.get());
        });
    }


    /**
     * ThreadLocal 现象:
     * 主线程设置:1
     * pool-2-thread-1打印1 : null
     * pool-2-thread-2打印2 : null
     *
     * 多个子线程:
     *  无法获取主线程内容
     * */
    private static void threadLocalMany02() {
        ThreadLocal local = new ThreadLocal();

        local.set(1);
        System.out.println("主线程设置:"+local.get());

        executorMany.execute(()-> {
            String name = Thread.currentThread().getName();
            System.out.println(name+"打印1 : "+local.get());
        });
        executorMany.execute(()-> {
            String name = Thread.currentThread().getName();
            System.out.println(name+"打印2 : "+local.get());
        });
    }


    /**
     * ThreadLocal 现象:
     * 主线程设置:1
     * pool-1-thread-1打印1:null
     * pool-1-thread-1打印2:null
     *
     * 同一子线程:
     *  无法获取主线程内容
     * */
    private static void threadLocalOne01() {
        ThreadLocal local = new ThreadLocal();

        local.set(1);
        System.out.println("主线程设置:"+local.get());

        executorOne.execute(()-> {
            String name = Thread.currentThread().getName();
            System.out.println(name+"打印1:"+local.get());
        });
        executorOne.execute(()-> {
            String name = Thread.currentThread().getName();
            System.out.println(name+"打印2:"+local.get());
        });
    }


}

 

标签:name,get,System,TransmittableThreadLocal,println,local,out
From: https://www.cnblogs.com/zcjyzh/p/18555070

相关文章

  • 并发编程 (TransmittableThreadLocal)
    TransmittableThreadLocal的使用及原理解析一、基本使用首先,TTL是用来解决ITL解决不了的问题而诞生的,所以TTL一定是支持父线程的本地变量传递给子线程这种基本操作的,ITL也可以做到,但是前面有讲过,ITL在线程池的模式下,就没办法再正确传递了,所以TTL做出的改进就是即便是在线程池模......
  • TransmittableThreadLocal实现父子线程之间数据传递
    1、引入依赖<dependency><groupId>com.alibaba</groupId><artifactId>transmittable-thread-local</artifactId><version>2.14.5</version></dependency>2、传递String参数publicclassTransmittableThreadLocalDe......
  • ThreadLocal 家族:ThreadLocal、InheritableThreadLocal 与 TransmittableThreadLocal
    在当今的Java多线程编程领域,ThreadLocal及其相关的扩展InheritableThreadLocal和TransmittableThreadLocal宛如三把利剑,为我们在处理线程本地数据时提供了强大而灵活的工具。深入理解它们各自的特点、差异以及适用场景,对于我们编写出高效、可靠且稳定的多线程代码具有至......
  • 科普文:TransmittableThreadLocal通过javaAgent实现线程传递并支持ForkJoin
    概叙TransmittableThreadLocal的介绍,直接看官网:https://github.com/alibaba/transmittable-thread-local目前使用中的稳定发布版本v2.x在 分支2.x上。帮助文档:https://github.com/alibaba/transmittable-thread-local#-%E5%8A%9F%E8%83%BD这篇文章主要介绍了Transmitta......
  • TransmittableThreadLocal原理浅析
    TransmittableThreadLocal原理浅析1.从ThreadLocal到TransmittableThreadLocalTTL(TransmittableThreadLocal)是继承自InheritableThreadLocal,本质上也是一个ThreadLocal,可以理解为是一个可以在线程之间传递上下文的ThreadLocal。1.1ThreadLocalThreadLocal中填充的变量属于当......
  • 优秀开源(6)TransmittableThreadLocal
    序言 ThreadLocal存在的问题在多线程环境下,ThreadLocal可以避免线程安全问题,但是在使用线程池等多线程环境时,ThreadLocal可能会出现一些问题。例如,当使用线程池时,线程池中的线程可能会被多个任务共享,如果使用ThreadLocal存储数据,可能会导致数据被错误地共享。TransmittableTh......
  • 【TransmittableThreadLocal】TransmittableThreadLocal的实现机制和原理
    1 前言前面我看过了 ThreadLocal的实现机制和原理 以及 InheritableThreadLocal的实现机制和原理 两种类型的ThreadLocal,前者是普通的,后者是在前者的基础上套了一层父子线程关系,当使用后者的时候,会在线程创建的时候,浅拷贝一份父线程的变量值。那么今天空了,我来看看另外一......
  • TransmittableThreadLocal & InheritableThreadLocal
    InheritableThreadLocal类是ThreadLocal类的一个子类,它提供了一个线程局部变量,该变量的值可以被当前线程以及所有子线程共享。这在多线程编程中非常有用,特别是在需要在父线程和子线程之间传递数据时。下面是一个简单的Java代码示例,演示了InheritableThreadLocal的用法:publiccl......
  • TransmittableThreadLocal 的反复与纠缠
    TransmittableThreadLocal相信很多人用过,一个在多线程情况下共享线程上下文的利器名字好长,以下简称ttl本文以两年前一个真实项目开发遇到的问题,从源码的角度分析并解决环境itemversionjava8springboot2.2.2.RELEASEttl2.11.4代码如下,主线程并行启复......
  • TransmittableThreadLocal 如何解决在分布式环境下线程池中使用ThreadLocal的问题
    在分布式环境下,线程池中使用ThreadLocal会出现线程安全问题,因为线程池中的线程是可以被多个请求共享的,当多个请求同时访问同一个ThreadLocal变量时,会出现数据互相干扰的问题。为了解决这个问题,Java提供了TransmittableThreadLocal类。TransmittableThreadLocal是ThreadLocal的一......