我对执行者服务很陌生。喜欢自己做所有事情,但我认为是时候信任这些服务了。
我想通过 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...
发布评论
评论(4)
ExecutorCompletionService
http://download.oracle。 com/javase/1.5.0/docs/api/java/util/concurrent/ExecutorCompletionService.html
ExecutorCompletionService
http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ExecutorCompletionService.html
如果您想一个接一个地执行任务,最好在同一个线程中执行。
这样,它将在同一线程中的可运行之后执行您想要执行的任何操作,并且您不需要轮询或使用另一个线程。
If you want to do one task after another its better to do it in the same thread.
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.
我建议查看 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)。目前,您还可以将Runnable
s/Callable
包装在 ListenableFutureTask 自己。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 returnsListenableFuture
s using MoreExecutors.listeningDecorator(ExecutorService). You can also currently wrap yourRunnable
s/Callable
s in a ListenableFutureTask yourself.如果您可以具体假设使用 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.