java Callable FutureTask Excecuter:如何监听已完成的任务

发布于 2024-11-15 13:02:55 字数 466 浏览 3 评论 0 原文

我对执行者服务很陌生。喜欢自己做所有事情,但我认为是时候信任这些服务了。

我想通过 executor 提供一个 Runnable。执行器将其包装在 FutureTask 中并将其交还给我。现在我调用 poll done() 方法。但我希望在 done() 方法返回 true 时收到通知。

有一个 get() 方法会阻塞,直到 Runnable 完成,但随后我需要为每个作业添加一个额外的线程,只是为了查看它何时完成。

我可以给我的执行器一些额外的Callable来获得任务完成的通知吗?

到这里怎么走?我可以在 run 方法的末尾添加一些代码,但是 done() 可能仍然是 false...

I'm quite new to executer services. Liked doing everything myself, but I think it's time to trust these services.

I want to hand by Executer a Runnable. The executer wraps that in a FutureTask and hands it back to me. Now I call poll the done() method. But I would like to be notified when then done() method would return true.

There is a get() method that blocks until the Runnable has finished, but then I would need a extra thread for every job, just to see when it's finished.

Can I hand my executer some extra Callable to get notified about the task finishing?

What's the way to go here? I could add some code to the end of the run method, but then done() might still be false...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柠檬 2024-11-22 13:02:55

如果您想一个接一个地执行任务,最好在同一个线程中执行。

Executor executor =
final Runnable runnable = 
executor.execute(new Runnable() {
    public void run() {
         runnable.run();
         // do something after the run() has finished.
    }
 });

这样,它将在同一线程中的可运行之后执行您想要执行的任何操作,并且您不需要轮询或使用另一个线程。

If you want to do one task after another its better to do it in the same thread.

Executor executor =
final Runnable runnable = 
executor.execute(new Runnable() {
    public void run() {
         runnable.run();
         // do something after the run() has finished.
    }
 });

This way it will do whatever you want done after the runnable in the same thread and you don't need to poll or use another thread.

牛↙奶布丁 2024-11-22 13:02:55

我建议查看 Guava 中的“nofollow">com.google.common.util.concurrent 软件包,特别是<一href="http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/util/concurrent/ListenableFuture.html" rel="nofollow">ListenableFuture 类型和代码与之相关。

一旦下一个版本 (r10) 发布,就可以轻松创建一个使用 ListenableFuture 的 ExecutorService。 href="http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/util/concurrent/MoreExecutors.html#listeningDecorator%28java.util.concurrent.ExecutorService%29" rel=" nofollow">MoreExecutors.listeningDecorator(ExecutorService)。目前,您还可以将 Runnables/Callable 包装在 ListenableFutureTask 自己。

final ListenableFutureTask<?> task = new ListenableFutureTask<Object>(
    runnable, null);
executor.submit(task);
task.addListener(new Runnable() {
  public void run() {
    // do whatever
  }
}, listenerExecutor);

I'd advise taking a look at the com.google.common.util.concurrent package in Guava, specifically the ListenableFuture type and the code related to it.

Once the next release (r10) is out, it'll be easy to create an ExecutorService that returns ListenableFutures using MoreExecutors.listeningDecorator(ExecutorService). You can also currently wrap your Runnables/Callables in a ListenableFutureTask yourself.

final ListenableFutureTask<?> task = new ListenableFutureTask<Object>(
    runnable, null);
executor.submit(task);
task.addListener(new Runnable() {
  public void run() {
    // do whatever
  }
}, listenerExecutor);
伤感在游骋 2024-11-22 13:02:55

如果您可以具体假设使用 java.util.concurrent.ThreadPoolExecutor,那么您可以使用它的钩子方法; afterexecute() 和 beforeexecute()。

http://download.oracle.com/javase /6/docs/api/java/util/concurrent/ThreadPoolExecutor.html

它们不像 ListenableFuture 那样优雅,但如果您只需要一种类型的“侦听器”,那么它可能是一个足够的解决方案给定的执行器实例。

If you can make a specific assumption of using a java.util.concurrent.ThreadPoolExecutor, then you can use its hook methods; afterExecute() and beforeExecute().

http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html

They are not as elegant as the ListenableFuture, but it might be an adequate solution provided you need only one type of a "listener" for a given executor instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文