gpt4 book ai didi

java - 我们是否需要关闭 ExecutorService fixedThreadPool

转载 作者:搜寻专家 更新时间:2023-11-01 03:02:39 24 4
gpt4 key购买 nike

我在我的应用程序中使用 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)));
}

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

最佳答案

  • 新固定线程池

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很好的解释。

关于java - 我们是否需要关闭 ExecutorService fixedThreadPool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31703865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com