可调用执行期间的exception
我有以下Callable:
public class Worker implements Callable{ @Override public Boolean call(){ boolean success=true; //do Something return success; } }
现在我正在执行它:
Worker worker - new Worker(); Future submit = executor.submit(worker);
我正在以一种hashMap的forms存储提交,以便在代码中的某个地方执行某些操作。
我怎么知道worker.call()
函数中是否发生了exception?
如果出现某种exception, submit.isCancelled()
返回true,如果一切正常,则返回false吗?
当你调用Future.get()
它会把你的exception包装在ExecutionException
。
如文档中所述, Future
将重新抛出isCancelled()
期间发生的exception(虽然包装在ExecutionException
),并且在这种情况下isCancelled()
仍然为false。
在Guava Librairies中 ,有一个FutureCallback
接口,它有一个onFailure(Throwable t)
方法,你应该对它感兴趣
将Google Guava Librairies添加到项目中绝不是浪费空间:-)
submit.get()将抛出ExecutionException