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 * @version 1.0 October 2009
\r
44 class _TrackingExecutor extends AbstractExecutorService {
\r
46 private final ExecutorService executor;
\r
48 public _TrackingExecutor(ExecutorService executor) {
\r
49 this.executor = executor;
\r
52 private final Set<Runnable> cancelledRunnableTasksAtShutdown = new HashSet<Runnable>();
\r
53 private final Set<Callable<?>> cancelledCallableTasksAtShutdown = new HashSet<Callable<?>>();
\r
55 public List getCancelledTasks() {
\r
56 if (!executor.isTerminated()) {
\r
57 throw new IllegalStateException(
\r
58 "Executor must be terminated before running this method!");
\r
60 ArrayList tasks = new ArrayList(cancelledCallableTasksAtShutdown);
\r
61 tasks.addAll(cancelledRunnableTasksAtShutdown);
\r
66 public void execute(final Runnable runnable) {
\r
67 executor.execute(new Runnable() {
\r
73 if (isShutdown() && Thread.currentThread().isInterrupted()) {
\r
74 cancelledRunnableTasksAtShutdown.add(runnable);
\r
82 public <T> Future<T> submit(final Callable<T> task) {
\r
83 return executor.submit(new Callable<T>() {
\r
85 public T call() throws Exception {
\r
89 if (isShutdown() && Thread.currentThread().isInterrupted()) {
\r
90 cancelledCallableTasksAtShutdown.add(task);
\r
98 public boolean awaitTermination(long timeout, TimeUnit unit)
\r
99 throws InterruptedException {
\r
100 return executor.awaitTermination(timeout, unit);
\r
104 public boolean isShutdown() {
\r
105 return executor.isShutdown();
\r
109 public boolean isTerminated() {
\r
110 return executor.isTerminated();
\r
114 public void shutdown() {
\r
115 executor.shutdown();
\r
119 public List<Runnable> shutdownNow() {
\r
120 return executor.shutdownNow();
\r