tempus-fugit

Java micro-library for writing & testing concurrent code

Schedule Interruption (Wake a Sleeping Thread)

The Interrupter class allows you to schedule an interrupt on a thread after a specified duration. This can be useful when implementing timeouts on classes that support the use of interrupt as an interruption policy. For example, the code below sets up an interrupt to be scheduled after some timeout, before embarking on some potentially long running process. The Interrupter and Thread classes have been statically imported.

1
2
3
4
5
6
7
8
Interrupter interrupter = interrupt(currentThread()).after(timeout);
try {
    while (!currentThread().isInterrupted()) {
        // some long running process
    }
} finally {
    interrupter.cancel();
}

The Interrupter spawns a thread which sleeps (using WaitFor) until the timeout expires. It then just calls interrupt on the passed in thread. It is important therefore to ensure you cancel the interrupt as above for the case when the long running process could finish before the timeout. The cancel has no affect if the timeout has already expired so using a finally block is recommended.

The DefaultTimeoutableCompletionService classes uses this approach to implement a java.util.concurrent.CompletionService-like service that will timeout and return any completed tasks and abandoning any remaining.

Next, Thread Utilities: Thread Dumps »