Java 我们需要关闭ExecutorService fixedThreadPool吗
我已经在我的应用程序中使用Java 我们需要关闭ExecutorService fixedThreadPool吗,java,executorservice,threadpoolexecutor,Java,Executorservice,Threadpoolexecutor,我已经在我的应用程序中使用ExecutorService创建了线程池,以调用供应商Web服务,使用下面的代码 ExecutorService executor = Executors.newFixedThreadPool(getThreadPoolSize()); for (int i = 0; i < list.size(); i++) { Request ecpReq = list.get(i);
ExecutorService
创建了线程池,以调用供应商Web服务,使用下面的代码
ExecutorService executor = Executors.newFixedThreadPool(getThreadPoolSize());
for (int i = 0; i < list.size(); i++) {
Request ecpReq = list.get(i);
thRespLst.add(executor.submit(new Task(ecpReq)));
}
ExecutorService executor=Executors.newFixedThreadPool(getThreadPoolSize());
对于(int i=0;i
我想知道我们是否需要关闭threadpool之类的东西,基本上我不希望在生产环境中挂起线程 - newFixedThreadPool
这是一个很好的解释。
FinalizableDelegatedExecutorService
和ThreadPoolExecutor
覆盖finalize()
以执行关机操作
/**
* Invokes {@code shutdown} when this executor is no longer
* referenced and it has no threads.
*/
protected void finalize() {
shutdown();
}
实际上,我认为没有理由显式关闭
ExecutorService
Javadoc说:“应该关闭未使用的ExecutorService
,以便回收其资源。”您的迭代不是线程安全的:调用list.size()
可能会返回一个数字N
,然后,在调用list.get(i)
之前,n
元素可能已被删除。必须显式关闭ExecutorService以回收已完成作业但仍存在的线程占用的资源(CPU和内存)。