我们是否需要关闭 ExecutorService fixedThreadPool
Do we need to shutdown ExecutorService fixedThreadPool
我在我的应用程序中使用 ExecutorService
创建了线程池,使用以下代码调用供应商 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)
Creates a thread pool that reuses a fixed number of threads operating
off a shared unbounded queue. At any point, at most nThreads threads
will be active processing tasks. If additional tasks are submitted
when all threads are active, they will wait in the queue until a
thread is available. If any thread terminates due to a failure during
execution prior to shutdown, a new one will take its place if needed
to execute subsequent tasks. The threads in the pool will exist until
it is explicitly shutdown.
Javadocs.
Here很好的解释。
FinalizableDelegatedExecutorService
和 ThreadPoolExecutor
覆盖 finalize()
以执行关机。
/**
* Invokes {@code shutdown} when this executor is no longer
* referenced and it has no threads.
*/
protected void finalize() {
shutdown();
}
实际上,我认为没有理由明确关闭 ExecutorService
。
我在我的应用程序中使用 ExecutorService
创建了线程池,使用以下代码调用供应商 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)
Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.
Javadocs.
Here很好的解释。
FinalizableDelegatedExecutorService
和 ThreadPoolExecutor
覆盖 finalize()
以执行关机。
/**
* Invokes {@code shutdown} when this executor is no longer
* referenced and it has no threads.
*/
protected void finalize() {
shutdown();
}
实际上,我认为没有理由明确关闭 ExecutorService
。