1 /* Copyright (c) 2009 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0
\r
5 * This library is free software; you can redistribute it and/or modify it under the terms of the
\r
6 * Apache License version 2 as published by the Apache Software Foundation
\r
8 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
\r
9 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache
\r
10 * License for more details.
\r
12 * A copy of the license is in apache_license.txt. It is also available here:
\r
13 * @see: http://www.apache.org/licenses/LICENSE-2.0.txt
\r
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
18 package compbio.engine.local;
\r
20 import java.util.ArrayList;
\r
21 import java.util.HashSet;
\r
22 import java.util.List;
\r
23 import java.util.Set;
\r
24 import java.util.concurrent.AbstractExecutorService;
\r
25 import java.util.concurrent.Callable;
\r
26 import java.util.concurrent.ExecutorService;
\r
27 import java.util.concurrent.Future;
\r
28 import java.util.concurrent.TimeUnit;
\r
31 * This executor extends standard Java ExecutorService by adding the method to
\r
32 * obtain all Runnables which were running and did not complete upon executor
\r
33 * termination. For this to work properly Runnables must propagate an
\r
34 * Interruption exceptions, not swallow them, which a good Runnable should do
\r
37 * TODO it may be better to persists task from different place
\r
40 * @Date October 2009
\r
42 public class _TrackingExecutor extends AbstractExecutorService {
\r
44 private final ExecutorService executor;
\r
46 public _TrackingExecutor(ExecutorService executor) {
\r
47 this.executor = executor;
\r
50 private final Set<Runnable> cancelledRunnableTasksAtShutdown = new HashSet<Runnable>();
\r
51 private final Set<Callable<?>> cancelledCallableTasksAtShutdown = new HashSet<Callable<?>>();
\r
53 public List getCancelledTasks() {
\r
54 if (!executor.isTerminated()) {
\r
55 throw new IllegalStateException(
\r
56 "Executor must be terminated before running this method!");
\r
58 ArrayList tasks = new ArrayList(cancelledCallableTasksAtShutdown);
\r
59 tasks.addAll(cancelledRunnableTasksAtShutdown);
\r
64 public void execute(final Runnable runnable) {
\r
65 executor.execute(new Runnable() {
\r
71 if (isShutdown() && Thread.currentThread().isInterrupted()) {
\r
72 cancelledRunnableTasksAtShutdown.add(runnable);
\r
80 public <T> Future<T> submit(final Callable<T> task) {
\r
81 return executor.submit(new Callable<T>() {
\r
83 public T call() throws Exception {
\r
87 if (isShutdown() && Thread.currentThread().isInterrupted()) {
\r
88 cancelledCallableTasksAtShutdown.add(task);
\r
96 public boolean awaitTermination(long timeout, TimeUnit unit)
\r
97 throws InterruptedException {
\r
98 return executor.awaitTermination(timeout, unit);
\r
102 public boolean isShutdown() {
\r
103 return executor.isShutdown();
\r
107 public boolean isTerminated() {
\r
108 return executor.isTerminated();
\r
112 public void shutdown() {
\r
113 executor.shutdown();
\r
117 public List<Runnable> shutdownNow() {
\r
118 return executor.shutdownNow();
\r