いくつかのメソッドを並行して呼び出しています。メソッドが例外をスローした場合、メソッドに渡された値を使用したい。
PS: 構文エラーは無視してください。
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
}
}