我在我的应用程序中使用 ExecutorService 创建了threadpool,使用下面的代码调用供应商websrvice。
I have created threadpool using ExecutorService, in my application to call vendor websrvice, using below code.
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))); }我们需要知道我们需要关注线程池或其他东西,基本上我不想在生产环境中悬挂线程。
Wanted to know do we need to take care of shutting down threadpool or something, basically I don't want hanging threads in production environment.
推荐答案
- newFixedThreadPool
- newFixedThreadPool
public static ExecutorService newFixedThreadPool(int nThreads)
public static ExecutorService newFixedThreadPool(int nThreads)
创建一个重用a的线程池在共享的无界队列中运行的固定线程数。在任何时候,最多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。
这里 a很好的解释。
更多推荐
我们是否需要关闭ExecutorService fixedThreadPool
发布评论