5

I have checked Oracle Java API, which gives some info that FutureTask.isDone()

but I need to check whether the task has completed or terminated with any error. isDone() method will return even if it completes/terminates. But I need to know whether it's completed or terminated due to some problem.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
RaceBase
  • 18,428
  • 47
  • 141
  • 202

3 Answers3

12

If the FutureTask is done, call get() afterwards (can be without any timeout, it should return immediately). It will either return some result or throw:

ExecutionException - if the computation threw an exception

ExecutionException.getCause() will return the exception thrown from your task.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • You can call `get()` even if `FutureTask` is not yet done; in this case `get()` waits for completion. – Pino Oct 20 '16 at 14:18
0

Why not call FutureTask.get() ?

This will return the result or throw an exception containing the exception originally thrown/caught. If the task hasn't complete dit will block, and you have the option to provide a timeout.

I would normally expect clients simply to call get() on each FutureTask in turn once created, rather than poll isDone().

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
0

When isDone() return true you would like to get result by calling get() method to check the result of execution. get() operation can throw ExecutionException, which consist of cause, where you can find a problem.

gkuzmin
  • 2,414
  • 17
  • 24
  • 1
    You can call `get()` even if `isDone()` is yet false; in this case `get()` waits for completion. – Pino Oct 20 '16 at 14:19