删除ThreadPoolExecutor的所有排队任务 [英] Removing all queued tasks of an ThreadPoolExecutor

查看:1160
本文介绍了删除ThreadPoolExecutor的所有排队任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于的这个相当简单的问题ThreadPoolExecutor的。我有以下情况:我必须从队列中使用对象,为它们创建适当的工作任务并将它们提交给ThreadPoolExecutor。这很简单。但是在关闭场景中许多工作人员可能会排队等待执行。由于其中一个任务可能正在运行一个小时,并且我希望相对快速正常关闭应用程序,我想从ThreadPoolExecutor中丢弃所有排队的任务,而已处理的任务应该正常完成。

i have this rather simple question about the ThreadPoolExecutor. I have the following situation: I have to consume objects from a queue, create the appropiate worker tasks for them and submit them to the ThreadPoolExecutor. This is quite simple. But within a shutdown scenario many workers may be queued to execution. Since one of those tasks might be running for an hour, and i want a relativly fast graceful shutdown of the application i want to discard all queued tasks from the ThreadPoolExecutor while the already processing tasks should be completed normally.

ThreadPoolExecutor文档中有一个 remove()方法,但只允许删除特定任务。 清除()仅适用于已取消的Future任务。我的想法是清除队列中包含所有排队的任务。 ThreadPoolExecutor提供对此内部队列的访问,但文档说明:

The ThreadPoolExecutor documentation has a remove() method but only allows specific tasks to be removed. purge() only works for already canceled Future tasks. My idea was to clear the queue holding all queued tasks. The ThreadPoolExecutor provides access to this internal queue but the documentation states:


方法getQueue()允许访问
工作队列监视
和调试的目的。使用此方法
用于任何其他目的是强烈建议

Method getQueue() allows access to the work queue for purposes of monitoring and debugging. Use of this method for any other purpose is strongly discouraged.

因此抓住此队列并清除它不是一种选择。此外,文档的这个片段说:

So grabbing this queue and clearing it is not an option. Also, this snippet of the documentation says:


两个提供的方法,
remove(java.lang.Runnable)和purge( )
可用于在大量
排队任务被取消时协助存储
回收。

Two supplied methods, remove(java.lang.Runnable) and purge() are available to assist in storage reclamation when large numbers of queued tasks become cancelled.

如何?当然,我可以维护我提交给执行程序的所有任务的列表,在关闭的情况下,我遍历所有条目并使用remove()方法从ThreadPoolExecutor中删除它们......但是...来吧,这是一个浪费记忆力和维护这份清单的麻烦。 (例如删除已执行的任务)

How? Sure, i can maintain a list of all tasks i submitted to the executor and in a shutdown case i iterate over all entries and remove them from the ThreadPoolExecutor with the remove() method... but... come on, this is a waste of memory and a hassle to maintain this list. (Removing already executed tasks for example)

我感谢任何提示或解决方案!

I appreciate any hints or solutions!

推荐答案

您是否考虑过包装ExecutorService?创建

Have you considered wrapping the ExecutorService? Create a

CleanShutdownExecutorService implements Executor 

将所有调用委托给另一个Executor,但将Futures保留在自己的列表中。然后,CleanShutdownExecutorService可以使用cancelRemainingTasks()方法调用shutdown(),然后在其列表中的所有Futures上调用cancel(false)。

that delegates all calls to another Executor, but keeps the Futures in a list of its own. CleanShutdownExecutorService can then have a cancelRemainingTasks() method that calls shutdown(), then calls cancel(false) on all the Futures in its list.

这篇关于删除ThreadPoolExecutor的所有排队任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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