diff --git a/common/source/java/ch/systemsx/cisd/common/concurrent/TerminableCallable.java b/common/source/java/ch/systemsx/cisd/common/concurrent/TerminableCallable.java index a857ecde5060d047bca1252a9b2196107590a876..abc8e2fc42f08e7969485c0911275d0ae6e9d6b8 100644 --- a/common/source/java/ch/systemsx/cisd/common/concurrent/TerminableCallable.java +++ b/common/source/java/ch/systemsx/cisd/common/concurrent/TerminableCallable.java @@ -183,9 +183,10 @@ public final class TerminableCallable<V> implements Callable<V>, ITerminable * Note that this method is <i>always</i> called, no matter what the cause is. If you want * to perform clean up only for some causes, check <var>cause</var> first. * <p> - * Note that the current Thread may be in an interrupted state when this method is called or - * may get interrupted during the method runs. It is advised not to use methods that throw - * an {@link InterruptedException} or equivalent in this method. + * <i>It is guaranteed that, if the callable is terminated with the + * {@link TerminableCallable#terminate(long)} or the {@link TerminableCallable#terminate()} + * method, this call will be interrupted by neither {@link Thread#interrupt()} nor + * <code>Thread.stop()</code>.</i> * <p> * <strong>Don't perform any time consuming operations in this method and avoid any * operations that can fail with an exception.</strong> diff --git a/common/source/java/ch/systemsx/cisd/common/concurrent/ThreadGuard.java b/common/source/java/ch/systemsx/cisd/common/concurrent/ThreadGuard.java index df3414b417f4cebcb63e2204c964f6faec26d6e4..9691503906e62c56ea536baa57dd25929c5db4f9 100644 --- a/common/source/java/ch/systemsx/cisd/common/concurrent/ThreadGuard.java +++ b/common/source/java/ch/systemsx/cisd/common/concurrent/ThreadGuard.java @@ -78,13 +78,14 @@ final class ThreadGuard } } - private synchronized Thread getThreadForTermination() + private synchronized Thread tryInterruptAndGetThread() { if (state == State.RUNNING) { final Thread t = thread; thread = null; state = State.TERMINATING; + t.interrupt(); return t; } else { @@ -159,6 +160,7 @@ final class ThreadGuard { state = State.FINISHING; thread = null; + Thread.interrupted(); // Clear interrupted flag in case we are in mode TERMINATING. } /** @@ -237,10 +239,9 @@ final class ThreadGuard return true; } final long start = System.currentTimeMillis(); - final Thread t = getThreadForTermination(); + final Thread t = tryInterruptAndGetThread(); if (t != null) { - t.interrupt(); if (waitForFinished(waitInterruptMillis)) { return true;