Search code examples
javafuturetask

Java FutureTask completion check


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.


Solution

  • 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.