【发布时间】:2016-10-23 12:37:50
【问题描述】:
我是 ExecutorService 的 Spring 新手。请问你能确认下面的代码吗?我已经在@PostConstruct 方法中初始化了ExecutorSerive,并在@PreDestroy 方法中对其进行了破坏。是否需要调用shutdownNow()。如果有,会在哪里?
private static final int FIXED_THREAD_POOL_SIZE =3;
private static final Log LOGGER = LogFactory.getLog(HotelProgramInfoHistoryService.class);
private ExecutorService executorService;
@PostConstruct
public void initialize() throws Exception {
if(executorService == null) {
this.executorService = Executors.newFixedThreadPool(FIXED_THREAD_POOL_SIZE);
}
}
public void sendRequest(DomainSecurity security, HotelProgramInfo hotelProgramInfo) {
try {
ProgramInfoHistoryUpdateTask programInfoHistoryUpdateTask = new ProgramInfoHistoryUpdateTask(hotelProgramInfo);
this.executorService.submit(programInfoHistoryUpdateTask);
} catch (Exception exception) {
LOGGER.error("Exception occurred while submitting update task ", exception);
}
}
@PreDestroy
public void cleanUp() throws Exception {
if(executorService != null)
{
executorService.shutdown();
}
}
}
【问题讨论】:
标签: java spring multithreading