我们是否需要关闭ExecutorService fixedThreadPool

编程入门 行业动态 更新时间:2024-10-27 00:26:52
本文介绍了我们是否需要关闭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

本文发布于:2023-07-07 16:12:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1065202.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ExecutorService   fixedThreadPool

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!