问题
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.
回答1:
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.
回答2:
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()
.
回答3:
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.
来源:https://stackoverflow.com/questions/11776908/java-futuretask-completion-check