/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ package org.ow2.choreos.utils; import java.util.List; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import org.apache.log4j.Logger; public class Concurrency { public static void waitExecutor(ExecutorService executor, int timeout, TimeUnit timeUnit, Logger logger, String errorMessage) { executor.shutdown(); boolean status = false; try { status = executor.awaitTermination(timeout, timeUnit); } catch (InterruptedException e) { logErrorMessage(errorMessage, logger); } if (!status) { logErrorMessage(errorMessage, logger); } executor.shutdownNow(); } public static void waitExecutor(ExecutorService executor, int timeoutMinutes, String errorMessage) { waitExecutor(executor, timeoutMinutes, TimeUnit.MINUTES, null, errorMessage); } public static void waitExecutor(ExecutorService executor, int timeoutMinutes, Logger logger, String errorMessage) { waitExecutor(executor, timeoutMinutes, TimeUnit.MINUTES, logger, errorMessage); } private static void logErrorMessage(String msg, Logger logger) { if (logger != null) { logger.error(msg); } else { System.out.println(msg); } } /** * Returns the future return or throws an exception. * * If there is no exception and future is not done, future is canceled. * * @param f * @return f.get() * @throws ExecutionException * if the Callable code caused the exception, or * IllegalStateException if there was any other problem */ public static <T> T checkAndGetFromFuture(Future<T> f) throws ExecutionException { T result = null; try { if (f.isDone()) { result = f.get(); } else { f.cancel(true); throw new IllegalStateException("Future not done, therefore canceled"); } } catch (InterruptedException e) { throw new IllegalStateException(e); } catch (ExecutionException e) { throw e; } catch (CancellationException e) { throw new IllegalStateException(e); } return result; } public static void waitThreads(List<Thread> threads) { for (Thread trd : threads) { try { trd.join(); } catch (InterruptedException e) { System.out.println("Vish! Thread died at sleep!"); } } } }