Java tutorial
//package com.java2s; /* * Copyright 2012-2014 David Karnok * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; public class Main { /** * Await the completion of tasks interruptibly. The execution * exceptions are combined into a single exception, but interrupted * exception breaks the wait. * @param tasks the tasks to wait * @throws ExecutionException on execution errors * @throws InterruptedException when the wait is interrupted */ public static void awaitAllInterruptibly(Iterable<? extends Future<?>> tasks) throws ExecutionException, InterruptedException { ExecutionException ee = null; for (Future<?> f : tasks) { try { f.get(); } catch (ExecutionException ex) { if (ee != null) { ee.addSuppressed(ex); } else { ee = ex; } } } if (ee != null) { throw ee; } } }