java 停止提交给 ExecutorService 的 Runnable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13929618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:29:14  来源:igfitidea点击:

Stop a Runnable submitted to ExecutorService

javaexecutorservice

提问by maverik

I've implemented subscription in my Java app. When new subscriber added, the application creates new task (class which implements Runnableto be run in the separate thread) and it is added to the ExecutorServicelike:

我已经在我的 Java 应用程序中实现了订阅。添加新订阅者时,应用程序会创建新任务(实现Runnable在单独线程中运行的类),并将其添加到以下内容中ExecutorService

public void Subscribe()
{
    es_.execute(new Subscriber(this, queueName, handler));
}

//...

private ExecutorService es_;

Application may register as many subscribers as you want. Now I want implement something like Unsubscribeso every subscriber has an ability to stop the message flow. Here I need a way to stop one of the tasks running in the ExecutorService. But I don't know how I can do this.

应用程序可以根据需要注册任意数量的订阅者。现在我想要实现类似的东西,Unsubscribe让每个订阅者都有能力停止消息流。在这里,我需要一种方法来停止在ExecutorService. 但我不知道我怎么能做到这一点。

The ExecutorService.shutdown()and its variations are not for me: they terminates all the tasks, I want just terminate one of them. I'm searching for a solution. As simple as possible. Thanks.

TheExecutorService.shutdown()及其变体不适合我:它们终止所有任务,我只想终止其中一个。我正在寻找解决方案。尽可能简单。谢谢。

回答by Aviram Segal

You can use ExecutorService#submitinstead of executeand use the returned Futureobject to try and cancel the task using Future#cancel

您可以使用ExecutorService#submit而不是execute使用返回的Future对象来尝试使用Future#cancel 取消任务

Example (Assuming Subscriberis a Runnable):

示例(假设Subscriber是 a Runnable):

Future<?> future = es_.submit(new Subscriber(this, queueName, handler));
...
future.cancel(true); // true to interrupt if running


Important note from the comments:

评论中的重要说明:

If your task doesn't honour interrupts and it has already started, it will run to completion.

If your task doesn't honour interrupts and it has already started, it will run to completion.

回答by Florin

Instead of using ExecutorService.execute(Runnable)try using Future<?> submit(Runnable). This method will submit the Runnableinto the pool for execution and it will return a Futureobject. By doing this you will have references to all subscriber threads.

而不是使用ExecutorService.execute(Runnable)尝试使用Future<?> submit(Runnable)。该方法将提交Runnable到池中执行,并返回一个Future对象。通过这样做,您将拥有对所有订阅者线程的引用。

In order to stop particular thread just use futureObj.cancel(true). This will interrupt the running thread, throwing an InterruptedException. The subscriber thread should be coded such way it will stop processing in the case of this exception (for example Thread.sleep(millis)with wrapper try / catch block for the whole method).

为了停止特定线程,只需使用futureObj.cancel(true). 这将中断正在运行的线程,抛出一个InterruptedException. 订阅者线程的编码方式应使其在出现此异常时停止处理(例如Thread.sleep(millis),整个方法的包装器 try / catch 块)。

You cand find more information on the official API: http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/Future.htmlhttp://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html

您可以找到有关官方 API 的更多信息:http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/Future.html http://docs.oracle.com/javase/6 /docs/api/java/util/concurrent/ExecutorService.html