Разве объект, возвращаемый ThreadPoolTaskExecutor.getThreadPoolExecutor(), не тот же самый?JAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Разве объект, возвращаемый ThreadPoolTaskExecutor.getThreadPoolExecutor(), не тот же самый?

Сообщение Anonymous »

Я сам инициализировал компонент для ThreadPoolTaskExecutor.
Затем вызовите 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;
}
}
Затем используйте BeanPostProcessor, чтобы получить его.

Код: Выделить всё

@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
ThreadPoolTaskExecutorinitialize был вызван в методе AsyncConfigUtil.createThreadPoolTaskExecutor().
Я проверил исходный код инициализацииExecutor, и ни в одном другом месте исполнитель повторно не инициализировался.
Какие обстоятельства отличают их?>

Подробнее здесь: https://stackoverflow.com/questions/798 ... ecutor-the
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»