Ist das von ThreadPoolTaskExecutor.getThreadPoolExecutor() zurückgegebene Objekt nicht dasselbe?Java

Java-Forum
Anonymous
 Ist das von ThreadPoolTaskExecutor.getThreadPoolExecutor() zurückgegebene Objekt nicht dasselbe?

Post by Anonymous »

Ich habe selbst ein Bean für ThreadPoolTaskExecutor initialisiert.
Dann rufen Sie threadPoolTaskExecutor.getThreadPoolExecutor().hashCode() auf, um den neuesten hasCode zu erhalten.

Code: Select all

@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;
}
}
Dann verwenden Sie BeanPostProcessor, um ihn abzurufen.

Code: Select all

@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;
}

}
Der gedruckte HashCode ist anders.

Code: Select all

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
ThreadPoolTaskExecutorinitialize wurde in der Methode AsyncConfigUtil.createThreadPoolTaskExecutor() aufgerufen.
Ich habe den Quellcode von initializeExecutor überprüft und an keiner anderen Stelle wurde der Executor neu initialisiert.
Welche Umstände unterscheiden sie?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post