我们是否需要关闭ExecutorService fixedThreadPool [英] Do we need to shutdown ExecutorService fixedThreadPool

查看:427
本文介绍了我们是否需要关闭ExecutorService fixedThreadPool的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆