public final class InterruptTimer extends Object
Classes can use this to trip an alarm interrupting the calling thread if it doesn't complete a block within the specified timeout. Typical calling pattern is:
private InterruptTimer myTimer = ...; void foo() { try { myTimer.begin(timeout); // work } finally { myTimer.end(); } }
An InterruptTimer is not recursive. To implement recursive timers, independent InterruptTimer instances are required. A single InterruptTimer may be shared between objects which won't recursively call each other.
Each InterruptTimer spawns one background thread to sleep the specified time
and interrupt the thread which called begin(int)
. It is up to the
caller to ensure that the operations within the work block between the
matched begin and end calls tests the interrupt flag (most IO operations do).
To terminate the background thread, use terminate()
. If the
application fails to terminate the thread, it will (eventually) terminate
itself when the InterruptTimer instance is garbage collected.
TimeoutInputStream
Constructor and Description |
---|
InterruptTimer()
Create a new timer with a default thread name.
|
InterruptTimer(String threadName)
Create a new timer to signal on interrupt on the caller.
|
public InterruptTimer()
public InterruptTimer(String threadName)
The timer thread is created in the calling thread's ThreadGroup.
threadName
- name of the timer thread.public void begin(int timeout)
timeout
- number of milliseconds before the interrupt should trigger.
Must be > 0.public void end()
public void terminate()
Copyright © 2013. All Rights Reserved.