Java FutureTask completion check

China☆狼群 提交于 2019-12-23 12:16:54

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!