ARTICLE AD BOX
I initialized a Bean for ThreadPoolTaskExecutor myself.
Then call threadPoolTaskExecutor.getThreadPoolExecutor().hashCode() to get the latest 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; } }Then use BeanPostProcessor to retrieve him.
@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; } }The printed hashCode is different.
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$1ThreadPoolTaskExecutorinitialize has been called in the AsyncConfigUtil.createThreadPoolTaskExecutor() method.
I checked the source code of the initializeExecutor, and no other place has reinitialized the executor.
What circumstances make them different?
