I'm using the java's Future class to execute a task, but the method isDone returns true if the task completed. Completion may be due to normal termination, an exception, or cancellation -- in all of these cases, this method will return true.
is there a way to know if it ended because an exception or because it finished successfully ?
When one submit a task to ExecutorService which is take a long running time, then it returns a Future object immediately. This Future object can be used for task completion and getting result of computation.
A FutureTask can be used to wrap a Callable or Runnable object. Because FutureTask implements Runnable , a FutureTask can be submitted to an Executor for execution. In addition to serving as a standalone class, this class provides protected functionality that may be useful when creating customized task classes.
A Future represents the result of an asynchronous computation. Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation.
When you call Future.get() method, there is 4 possible outcomes:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With