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
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class | |
| pack-priv static class | |
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final InterruptTimer. | |
| private final InterruptTimer. | |
| private final InterruptTimer. |
| Access | Constructor and Description |
|---|---|
| public | |
| public | InterruptTimer(String
name of the timer thread. threadName)Create a new timer to signal on interrupt on the caller. |
| Modifier and Type | Method and Description |
|---|---|
| public void | begin(int
number of milliseconds before the interrupt should trigger.
Must be > 0. timeout)Arm the interrupt timer before entering a blocking operation. |
| public void | |
| public void |
| autoKiller | back to summary |
|---|---|
| pack-priv final InterruptTimer. | |
| state | back to summary |
|---|---|
| private final InterruptTimer. | |
| thread | back to summary |
|---|---|
| private final InterruptTimer. | |
| InterruptTimer | back to summary |
|---|---|
| public InterruptTimer() Create a new timer with a default thread name. | |
| InterruptTimer | back to summary |
|---|---|
| public InterruptTimer(String threadName) Create a new timer to signal on interrupt on the caller. The timer thread is created in the calling thread's ThreadGroup.
| |
| begin | back to summary |
|---|---|
| public void begin(int timeout) Arm the interrupt timer before entering a blocking operation.
| |
| end | back to summary |
|---|---|
| public void end() Disable the interrupt timer, as the operation is complete. | |
| terminate | back to summary |
|---|---|
| public void terminate() Shutdown the timer thread, and wait for it to terminate. | |
| Modifier and Type | Field and Description |
|---|---|
| private Thread | |
| private long | |
| private boolean |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv synchronized void | |
| pack-priv synchronized void | |
| private static long | |
| public synchronized void | |
| pack-priv synchronized void |
| callingThread | back to summary |
|---|---|
| private Thread callingThread | |
| deadline | back to summary |
|---|---|
| private long deadline | |
| terminated | back to summary |
|---|---|
| private boolean terminated | |
| AlarmState | back to summary |
|---|---|
| pack-priv AlarmState() | |
| begin | back to summary |
|---|---|
| pack-priv synchronized void begin(int timeout) | |
| end | back to summary |
|---|---|
| pack-priv synchronized void end() | |
| now | back to summary |
|---|---|
| private static long now() | |
| run | back to summary |
|---|---|
| public synchronized void run() Implements java. Doc from java. Runs this operation.
| |
| terminate | back to summary |
|---|---|
| pack-priv synchronized void terminate() | |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| AlarmThread | back to summary |
|---|---|
| pack-priv AlarmThread(String name, InterruptTimer. | |
| Modifier and Type | Field and Description |
|---|---|
| private final InterruptTimer. |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| protected void | finalize()
Overrides java. Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. |
| state | back to summary |
|---|---|
| private final InterruptTimer. | |
| AutoKiller | back to summary |
|---|---|
| pack-priv AutoKiller(InterruptTimer. | |
| finalize | back to summary |
|---|---|
| protected void finalize() throws Throwable Overrides java. Doc from java. Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.
A subclass overrides the
When running in a Java virtual machine in which finalization has been
disabled or removed, the garbage collector will never call
The general contract of
The
The Java programming language does not guarantee which thread will
invoke the
After the
The
Any exception thrown by the | |