Затем вызовите threadPoolTaskExecutor.getThreadPoolExecutor().hashCode(), чтобы получить последний hasCode.
Код: Выделить всё
@Slf4j
@EnableAsync
@Configuration
public class AsyncConfig {
@Bean
public ThreadPoolTaskExecutor taskExecutor(ThreadPoolTaskExecutorBuilder builder) {
// init
ThreadPoolTaskExecutor threadPoolTaskExecutor = AsyncConfigUtil.createThreadPoolTaskExecutor("DefaultAsync-", builder);
ThreadPoolExecutor threadPoolExecutor = threadPoolTaskExecutor.getThreadPoolExecutor();
log.info("getThreadPoolExecutor().hashCode():{} class:{}", System.identityHashCode(threadPoolExecutor),threadPoolExecutor.getClass());
return threadPoolTaskExecutor;
}
}
Код: Выделить всё
@Slf4j
public class ThreadPoolAutoRegistration implements BeanPostProcessor {
@Override
public @Nullable Object postProcessAfterInitialization(@Nullable Object bean, @Nullable String beanName)
throws BeansException {
// Auto Registration ThreadPoolTaskExecutor
if (bean instanceof ThreadPoolTaskExecutor taskExecutor) {
ThreadPoolExecutor threadPoolExecutor = taskExecutor.getThreadPoolExecutor();
log.info("getThreadPoolExecutor().hashCode():{} class:{}", System.identityHashCode(threadPoolExecutor),threadPoolExecutor.getClass());
GracefulShutdownMonitorConfig.registerThreadPool(
beanName + "-" + taskExecutor.getThreadNamePrefix(),
taskExecutor.getThreadPoolExecutor()
);
}
return bean;
}
}
Код: Выделить всё
2026-01-13 14:33:51.714 INFO 56464 --- [ main][] com.zhiwandian.config.AsyncConfig : getThreadPoolExecutor().hashCode():271306390 class:class org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor$1
2026-01-13 14:33:51.727 INFO 56464 --- [ main][] c.zhiwandian.ThreadPoolAutoRegistration : getThreadPoolExecutor().hashCode():2079009730 class:class org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor$1
Я проверил исходный код инициализацииExecutor, и ни в одном другом месте исполнитель повторно не инициализировался.
Какие обстоятельства отличают их?>
Подробнее здесь: https://stackoverflow.com/questions/798 ... ecutor-the
Мобильная версия