@@ -0,0 +1,2383 @@
+/**%0A * %0A */%0Apackage codemining.util.parallel;%0A%0Aimport static com.google.common.base.Preconditions.checkArgument;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.List;%0Aimport java.util.concurrent.Callable;%0Aimport java.util.concurrent.ExecutionException;%0Aimport java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors;%0Aimport java.util.concurrent.Future;%0Aimport java.util.concurrent.TimeUnit;%0Aimport java.util.logging.Logger;%0A%0Aimport org.apache.commons.lang.exception.ExceptionUtils;%0A%0Aimport codemining.util.SettingsLoader;%0A%0A/**%0A * A wrapper around Java's thread pool using Future. Uses Callable so tasks can%0A * return values.%0A * %0A * @author Jaroslav Fowkes %3Cjaroslav.fowkes@ed.ac.uk%3E%0A * %0A */%0Apublic class FutureThreadPool %7B%0A%0A%09private static final Logger LOGGER = Logger%0A%09%09%09.getLogger(FutureThreadPool.class.getName());%0A%0A%09private final ExecutorService threadPool;%0A%09private List%3CFuture%3C?%3E%3E futures;%0A%0A%09public static final int NUM_THREADS = (int) SettingsLoader%0A%09%09%09.getNumericSetting(%22nThreads%22, Runtime.getRuntime()%0A%09%09%09%09%09.availableProcessors());%0A%0A%09/**%0A%09 * %0A%09 */%0A%09public FutureThreadPool() %7B%0A%09%09threadPool = Executors.newFixedThreadPool(NUM_THREADS);%0A%09%7D%0A%0A%09/**%0A%09 * Interrupt the execution of any future tasks, returning tasks that have%0A%09 * been interrupted.%0A%09 */%0A%09public List%3CRunnable%3E interrupt() %7B%0A%09%09return threadPool.shutdownNow();%0A%09%7D%0A%0A%09public void pushAll(final Collection%3CCallable%3C?%3E%3E tasks) %7B%0A%09%09futures = new ArrayList%3CFuture%3C?%3E%3E();%0A%09%09for (final Callable%3C?%3E task : tasks) %7B%0A%09%09%09futures.add(threadPool.submit(task));%0A%09%09%7D%0A%09%7D%0A%0A%09/**%0A%09 * Push a task to be executed.%0A%09 * %0A%09 * @param task%0A%09 */%0A%09public void pushTask(final Callable%3C?%3E task) %7B%0A%09%09checkArgument(!threadPool.isShutdown(),%0A%09%09%09%09%22Cannot submit task to thread pool that has already been shutdown.%22);%0A%09%09if (futures == null) %7B%0A%09%09%09futures = new ArrayList%3CFuture%3C?%3E%3E();%0A%09%09%7D%0A%09%09futures.add(threadPool.submit(task));%0A%09%7D%0A%0A%09public %3CT%3E void putCompletedTasks(Collection%3CT%3E outputs) %7B%0A%09%09threadPool.shutdown();%0A%09%09try %7B%0A%09%09%09threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);%0A%09%09%7D catch (final InterruptedException e) %7B%0A%09%09%09LOGGER.warning(%22Thread Pool Interrupted %22%0A%09%09%09%09%09+ ExceptionUtils.getFullStackTrace(e));%0A%09%09%7D%0A%09%09for (Future%3C?%3E future : futures) %7B%0A%09%09%09try %7B%0A%09%09%09%09outputs.add((T) future.get());%0A%09%09%09%7D catch (InterruptedException %7C ExecutionException e) %7B%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A%7D%0A

Add Future based thread pool that can return stuff