我们是否需要关闭ExecutorService fixedThreadPool

我使用ExecutorService创建了threadpool,在我的应用程序中使用下面的代码调用供应商websrvice。

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

想要知道我们需要关注线程池或其他东西,基本上我不想在生产环境中挂线程。

  • 的newFixedThreadPool

public static ExecutorService newFixedThreadPool(int nThreads)

创建一个线程池,该池重用在共享的无界队列中运行的固定数量的线程。 在任何时候,最多nThreads线程将是活动的处理任务。 如果在所有线程都处于活动状态时提交了其他任务,则它们将在队列中等待,直到线程可用。 如果任何线程由于在关闭之前执行期间的故障而终止,则如果需要执行后续任务,则新线程将取代它。 池中的线程将一直存在,直到它被明确关闭

Javadoc中。

这里有个很好的解释。

FinalizableDelegatedExecutorServiceThreadPoolExecutor重写finalize()以执行关闭。

 /** * Invokes {@code shutdown} when this executor is no longer * referenced and it has no threads. */ protected void finalize() { shutdown(); } 

实际上,我认为没有理由显式关闭ExecutorService