TransmittableThreadLocal解决使用线程池时父子线程拷贝问题

45 阅读1分钟

问题描述:
当父子线程间传递变量时,使用jdk提供ThreadLocal无法实现, 使用jdk提供的InheritableThreadLocal可以实现,但是有可能出现使用线程池开启子线程时,线程池中的InheritableThreadLocal存储的变量没有被回收,造成线程间变量串了;

解决方案: 使用阿里提供的TransmittableThreadLocal解决该问题,示例如下

@Configuration
@Slf4j
public class ThreadPoolConfig {
    public static final int CORE_POOL_SIZE = 2;
    public static final int MAXIMUM_POOL_SIZE = 4;
    public static final long KEEP_ALIVE_TIME = 3000L;
    public static final int WAIT_QUEUE_SIZE = 1000;
    
    @Bean(value = "almQueryThreadPool", destroyMethod = "shutdown")
    public ExecutorService  almQueryThreadPool() {
        // 线程工厂
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("alm-query-pool-%d")
                .setUncaughtExceptionHandler((thread, throwable) -> log.error("alm-query {} got exception", thread, throwable))
                .build();

        ExecutorService executor = new ThreadPoolExecutor(
                20,
                20,
                KEEP_ALIVE_TIME,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(WAIT_QUEUE_SIZE),
                threadFactory,
                new ThreadPoolExecutor.AbortPolicy()
        );
        //使用TtlExecutors包装线程池, 目前不支持入参ThreadPoolExecutor
        ExecutorService ttlExecutorService = TtlExecutors.getTtlExecutorService(executor);
        log.info("init almQueryThreadPool.");

        return ttlExecutorService;
    }
}