ThreadPoolTaskExecutor在执行线程时,存在一个
TaskDecorator配置,可以装饰线程类。
1. 源码分析
源码:org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor#initializeExecutor
@Override protected ExecutorService initializeExecutor(ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {
BlockingQueue < Runnable > queue = createQueue(this.queueCapacity);
ThreadPoolExecutor executor;
//配置了线程装饰器
if (this.taskDecorator != null) {
executor = new ThreadPoolExecutor(this.corePoolSize, this.maxPoolSize, this.keepAliveSeconds, TimeUnit.SECONDS, queue, threadFactory, rejectedExecutionHandler) {@Override public void execute(Runnable command) {
//装饰线程类
Runnable decorated = taskDecorator.decorate(command);
if (decorated != command) {
decoratedTaskMap.put(decorated, command);
}

本文详细解析了SpringThreadPoolTaskExecutor如何使用TaskDecorator装饰线程,以及在实际项目中如何配置,重点展示了如何在装饰器中访问主线程的ThreadLocal变量。
最低0.47元/天 解锁文章
246

被折叠的 条评论
为什么被折叠?



