How can I find out how the task completed when using the java Future class

I use the java Future class to complete the task, but the isDone method returns true if the task is completed. Termination may be due to normal termination, exception or cancellation - in all these cases, this method will return true.

is there any way to find out if this ended because an exception or because it ended successfully?

+5
source share
3 answers

When you call the Future.get () method, there are 4 possible results:

  • You get the result value
  • You get a CancellationException - if the calculation has been canceled (for example, called Future.cancel (true))
  • ExecutionException -
  • InterruptedException - (, executor.shutdownNow())
+6

- get , .

+2

I used it in my code and the only way to find out if a task completed successfully using get ()

0
source

All Articles