Pregunta

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.

¿Fue útil?

Solución

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.

Otros consejos

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().

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top