/* Copyright (c) 2009 Peter Troshin * * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0 * * This library is free software; you can redistribute it and/or modify it under the terms of the * Apache License version 2 as published by the Apache Software Foundation * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache * License for more details. * * A copy of the license is in apache_license.txt. It is also available here: * @see: http://www.apache.org/licenses/LICENSE-2.0.txt * * Any republication or derived work distributed in source code form * must include this copyright and license notice. */ package compbio.engine.local; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.concurrent.AbstractExecutorService; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; /** * This executor extends standard Java ExecutorService by adding the method to * obtain all Runnables which were running and did not complete upon executor * termination. For this to work properly Runnables must propagate an * Interruption exceptions, not swallow them, which a good Runnable should do * anyway. * * TODO it may be better to persists task from different place * * @author pvtroshin * @version 1.0 October 2009 * @deprecated */ @Deprecated class _TrackingExecutor extends AbstractExecutorService { private final ExecutorService executor; public _TrackingExecutor(ExecutorService executor) { this.executor = executor; } private final Set cancelledRunnableTasksAtShutdown = new HashSet(); private final Set> cancelledCallableTasksAtShutdown = new HashSet>(); public List getCancelledTasks() { if (!executor.isTerminated()) { throw new IllegalStateException( "Executor must be terminated before running this method!"); } ArrayList tasks = new ArrayList(cancelledCallableTasksAtShutdown); tasks.addAll(cancelledRunnableTasksAtShutdown); return tasks; } @Override public void execute(final Runnable runnable) { executor.execute(new Runnable() { @Override public void run() { try { runnable.run(); } finally { if (isShutdown() && Thread.currentThread().isInterrupted()) { cancelledRunnableTasksAtShutdown.add(runnable); } } } }); } @Override public Future submit(final Callable task) { return executor.submit(new Callable() { @Override public T call() throws Exception { try { return task.call(); } finally { if (isShutdown() && Thread.currentThread().isInterrupted()) { cancelledCallableTasksAtShutdown.add(task); } } } }); } @Override public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { return executor.awaitTermination(timeout, unit); } @Override public boolean isShutdown() { return executor.isShutdown(); } @Override public boolean isTerminated() { return executor.isTerminated(); } @Override public void shutdown() { executor.shutdown(); } @Override public List shutdownNow() { return executor.shutdownNow(); } }