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