I have the following Java code:
final Future future = exeService.submit(
new Runnable() {
public void run() {
myObject.doSomething();
}
}
);
future.get();
where exeService
is an instance of
java.util.concurrent.ExecutorService
The problem is that myObject.doSomething()
never returns, and, hence, future.get()
never returns.
However, if I replace the call to submit
with a call to execute
like this:
exeService.execute(
new Runnable() {
public void run() {
myObject.doSomething();
}
}
);
the call to myObject.doSomething()
does return. I don t know if it matters, but doSomething()
is a void
method.
Why is doSomething()
finishing when using execute
but not when using submit
?
Also, I don t need to use Future.get()
; that just seemed to be the most natural way of doing this. (I also run into the same problem with CountdownLatch
.) The point is that I need to wait for doSomething()
to finish before proceeding, and, for complicated reasons I won t go into here, I need to launch it on a separate thread. If there is another way of doing this that works, that would be fine.