TransmittableThreadLocal

地址: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());
        });
    }


}

 

posted @ 2024-11-19 16:23  真某人  阅读(2)  评论(0编辑  收藏  举报