ThreadPoolExecutor:任务排队等待但未提交

编程入门 行业动态 更新时间:2024-10-12 05:44:24
本文介绍了ThreadPoolExecutor:任务排队等待但未提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们有一个场景,提交给ThreadPoolExecutor的任务长时间运行。当线程池启动时,我们启动它,核心池大小= 5,最大池大小= 20,队列大小为10.在我们的应用程序中,大约有10个任务被提交。大多数情况下,这些任务运行几分钟/小时然后完成。然而,有一种情况是所有5个任务都被I / O挂起。结果我的核心池大小达到了最大值,但我的Threadpoolexecutor队列未满。所以额外的5个任务永远不会有机会运行。 请建议我们如何处理这种情况?在这种情况下,有一个更小的队列更好的选择吗?初始化threadPool时最佳队列大小是什么?

We have a scenario where tasks submitted to ThreadPoolExecutor are long running. When the thread pool is started we start it with core pool size = 5, max pool size = 20 and queue size of 10. In our application around 10 tasks get submitted. Most of the time these tasks run for few mins/hrs and then complete. However there was a situation when all of the 5 tasks got hanged on I/O. As a result my core pool size reached it max, but my Threadpoolexecutor queue was not full. So the additional 5 tasks never got a chance to run. Please do suggest how we can handle such scenario? Is having a smaller queue better option in such situation? What would be an optimum queue size while initializing threadPool?

同样关于被挂起的任务,我们有什么方法可以将线程从Threadpool中拉出来?在这种情况下,至少其他任务将有机会运行。

Also regarding the hanged tasks, is there any way we can pull out the threads out of the Threadpool? In that case atleast other tasks would get a chance to run.

推荐答案

整体情况如下:

core pool size = 5, max pool size = 20 and queue size of 10

提交了10个任务。其中

10 tasks are submitted. Out of which

  • 5 I / O挂起的任务=>核心池大小的所有线程都被占用。因此没有空闲线程。
  • 5任务仍然存在。这5个线程入队排队,因为没有空闲线程,队列可以容纳10个任务。在队列已满或核心池中的任何线程都空闲之前,这些排队的任务将不会执行。
  • 因此,您的程序被绞死。

    Hence, Your Program is hanged .

    要了解更多关于动态的信息,请访问 c> ThrePoolExecutor oracle/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html\"rel =noreferrer> 此处 。这个文档的重点如下:

    To know more about dynamics of ThrePoolExecutor watch here . The notable points of this doc is as follows:

    • 如果运行的线程少于corePoolSize线程,则Executor总是更喜欢添加新线程而不是排队。
    • 如果corePoolSize或更多线程正在运行,Executor总是更喜欢排队请求而不是添加新线程。
    • 如果请求不能排队,创建一个新线程,除非这会超过maximumPoolSize,在这种情况下,任务将被拒绝。

    编辑 如果您希望增加核心池大小,那么您可以使用 setCorePoolSize(int corePoolSize) 。如果增加 corepoolsize ,则会启动新线程(如果需要)以执行任何排队任务。

    EDIT If you wish to increase core pool size then you can use setCorePoolSize(int corePoolSize) . If you increase the corepoolsize then new threads will, if needed, be started to execute any queued tasks.

    更多推荐

    ThreadPoolExecutor:任务排队等待但未提交

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

    发布评论

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

    >www.elefans.com

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