views:

150

answers:

2

Is it safe to assume that

java.util.concurrent.CompletionService.take().isDone() 

will always return true? If so, why does take() return a Future, rather than the value directly? If not, what does "completed task" as used in the documentation mean?

A: 

That's what the javadoc seems to imply, take() blocks until it can return a finished Future which of course will be done when it's returned.

Esko
I agree that's what it implies, but that leaves the question of why a Future is returned instead of the value directly. I should have duck-tested this question before asking it, though, because the instant I hit submit it occurred to me that success isn't the only condition that causes Future.isDone() to return true; it will also do so when the Future was cancelled. There's also value in being able to match the Future returned from take() to the corresponding Future returned previously returned from submit().
jfager
+1  A: 

Because you can use the Future for other things. You are given the future when you submit(). So you can first of all use the Future object returned to monitor the status or cancel prematurely. Second, if you change the futureFactory() (I think it is called something different but I have Java 5 not 6 installed) you can add all kinds of extra members to the future object.

So when doing the take(), you are right that in this case part of the future (the tracking/cancellation of state) isn't very useful. But OTOH, why not return the whole future as is, including extended functionality?

MJB
Also, there is the simple answer of design symmetry. While it's true if you are take() ing via completion service, the Future isn't so useful as a raw representation of task, it is used for all the other aspects of submission to Executor-type services, some of which it is critical in usage for.
MJB
Perfect, thanks.
jfager