我正在并行调用一些方法。如果有方法抛出异常,我想使用传递给方法的值。
请忽略任何语法错误。
public static void main() {
Executor executor = Executors.newFixedThreadPool(3);
CompletionService<SomeResult> executorCompletionService = new ExecutorCompletionService<SomeResult>(executor);
List<Future<Integer>> futures = new ArrayList<>();
futures.add(executorCompletionService.submit(() -> someMethod(parameterValueForFirstCall)));
futures.add(executorCompletionService.submit(() -> someMethod(parameterValueForSecondCall)));
futures.add(executorCompletionService.submit(() -> someMethod(parameterValueForThirdCall)));
for (int i=0; i<3; i++){
try {
executorCompletionService.take().get();
} catch(Exception e) {
System.out.println("exception caught :" + e.getMessage());
//do something with parameter passed to method that threw exception
}
}
1条答案
按热度按时间bn31dyow1#
像这样的?