Exception during Callable execution

…衆ロ難τιáo~ 提交于 2019-12-07 04:48:17

问题


I have the following Callable:

public class Worker implements Callable<Boolean>{

   @Override
   public Boolean call(){
      boolean success=true;

      //do Something
     return success;
   }

}

Now I'm executing it:

Worker worker - new Worker();
Future<Boolean> submit = executor.submit(worker);

I'm storing the submit in kind of hashMap for some operation to be performed somewhere in code.

How can I know if any exception has occured in worker.call() function?

Will submit.isCancelled() return true if some sort of Exception occurred and false if everything works ok?


回答1:


When you call Future.get() it will throw your exception wrapped in a ExecutionException.




回答2:


As stated in the documentation, Future<V> will re-throw the exception that occurred during .call() (though wrapped in an ExecutionException), and isCancelled() would still be false in this case.




回答3:


In Guava Librairies, there is a FutureCallback interface which has a onFailure(Throwable t) method which should interest you

Adding Google Guava Librairies to a project is never a waste of space :-)




回答4:


submit.get() will throw an ExecutionException



来源:https://stackoverflow.com/questions/8417903/exception-during-callable-execution

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