我们有一个场景,提交给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
因此,您的程序被绞死。
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:任务排队等待但未提交
发布评论