Java 如何在流API中处理InterruptedException和ExecutionException
我有一个项目列表,使用流API和从未来对象获取的值进行迭代。它引发编译时异常。在这种情况下如何处理 我已经尝试了包装方法,但问题仍然没有解决Java 如何在流API中处理InterruptedException和ExecutionException,java,Java,我有一个项目列表,使用流API和从未来对象获取的值进行迭代。它引发编译时异常。在这种情况下如何处理 我已经尝试了包装方法,但问题仍然没有解决 Future<Map<Integer, String>> serviceInfoFuture = executor.submit(new Callable<Map<Integer, String>>() { @Override p
Future<Map<Integer, String>> serviceInfoFuture = executor.submit(new Callable<Map<Integer, String>>() {
@Override
public Map<Integer, String> call() throws Exception {
return serviceUtil.getServiceInfoForTenant(Integer.parseInt(tenantId));
}
});
List<String> listOfServiceNameAndIds = response.getItems().stream().map(e -> uncheckCall(serviceInfoFuture.get().get(Integer.parseInt(e)+"="+e))).collect(Collectors.toList());
public static <T> T uncheckCall(Callable<T> callable) {
try { return callable.call(); }
catch (RuntimeException e) { throw e; }
catch (InterruptedException |ExecutionException e) { throw new RuntimeException(e); }
catch (Exception e){throw new RuntimeException(e);}
}
Future-serviceinfourture=executor.submit(新的可调用的(){
@凌驾
公共映射调用()引发异常{
返回serviceUtil.getServiceInfoForTenant(Integer.parseInt(tenantId));
}
});
List listOfServiceNameAndIds=response.getItems().stream().map(e->uncheckCall(serviceInfoFuture.get().get(Integer.parseInt(e)+“=”+e))).collect(Collectors.toList());
公共静态T取消选中调用(可调用可调用){
请尝试{return callable.call();}
catch(运行时异常e){throw e;}
catch(InterruptedException | ExecutionException e){throw new RuntimeException(e);}
catch(异常e){抛出新的RuntimeException(e);}
}
您没有处理服务信息未来.get()引发的中断异常
和执行异常
uncheckCall
仅处理从callable.call()
引发的异常
.map(e -> {
try {
return uncheckCall(serviceInfoFuture.get().get(Integer.parseInt(e) + "=" + e));
} catch (InterruptedException | ExecutionException exception) {
throw new RuntimeException(exception);
}
})