Callable执行期间异常 [英] Exception during Callable execution

查看:376
本文介绍了Callable执行期间异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Callable:

  public class Worker implements Callable< Boolean> {

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

//做某事
return success;
}

}



现在我执行它:

 工人工人 - 新工人
Future< Boolean> submit = executor.submit(worker);

我以hashMap的形式存储提交,以便在代码中执行某些操作。 / p>

如何知道 worker.call()函数中是否发生异常?



submit.isCancelled()如果发生某种异常则返回true,如果一切正常,返回false?

解决方案

当您调用 Future.get() 它会抛出你的异常包装在 ExecutionException


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?

解决方案

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

这篇关于Callable执行期间异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆