How to use an ExecutorCompletionService
I would like to use the constructor on the ExecutorCompletionService
which takes a pre-defined BlockingQueue
.
Can anyone provide a code snippet to show how this is done. It seems only to take a a BlockingQueue
with Future
s. How does this match with the fact that submit must be done with Callable
s.
I'm ge开发者_JAVA百科tting a little confused here - please help...
The Callables
supplied to submit()
will be wrapped up internally as FutureTask
s, and it's those (or a wrapper around one) that will eventually be inserted into the provided queue. That's all an implementation detail, though. (And, note, that this use case of inserting the outcome into the queue is the main reason why there's a protected done()
method in FutureTask
.) The contract says that result-yielding functions go in, and eventually results come out. The Future
s drawn from the queue represent the eventual outcomes of the functions you submitted earlier.
The javadoc for ExecutorCompletionService has a large code sample in it, and the documented constructor takes a BlockingQueue
.
精彩评论