当前位置:网站首页>Using transmittablethreadlocal to realize parameter cross thread transmission

Using transmittablethreadlocal to realize parameter cross thread transmission

2022-04-23 18:11:00 dawnsun001

  1. establish ThreadLocalContext
public class ThreadLocalContext {
    

    public static TransmittableThreadLocal<SystemParams> systemParamsThreadLocal = new TransmittableThreadLocal<>();
}
  1. assignment
ThreadLocalContext.systemParamsThreadLocal.set(systemParamsDTO);
  1. Thread pool configuration
@Configuration
public class ThreadPoolConfig {
    

    @Bean
    public ExecutorService threadPoolExecutor(){
    
        return  TtlExecutors.getTtlExecutorService(new ThreadPoolExecutor(
                10, 1000,30000L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(2000),
                new CustomizableThreadFactory("thread-pool-"),
                RejectPolicy.ABORT.getValue()));
    }
}

版权声明
本文为[dawnsun001]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230544288977.html