edu.rice.cs.plt.concurrent
Class DelayedInterrupter

java.lang.Object
  extended by edu.rice.cs.plt.concurrent.DelayedInterrupter

public class DelayedInterrupter
extends Object

Sets a "time bomb" on a specific thread: if the abort() method is not invoked within a specified amount of time (in milliseconds), the thread will be interrupted. In contrast to Object.wait(long), implementing a timeout in this way clearly distinguishes between a timeout-triggered wake-up and a Object.notify()-triggered or spurious wake-up. This alternative is also useful where the thread to timeout performs multiple blocking operations, invokes blocking APIs that don't support timeouts (like InputStream.read()), or polls for an interrupted state.

The timeout is implemented with a single Timer for all DelayedInterrupter instances. This timer is a daemon: an outstanding DelayedInterrupter will not prevent the program from terminating.


Constructor Summary
DelayedInterrupter(long timeToInterrupt)
          Create an interrupter for the current thread.
DelayedInterrupter(Thread worker, long timeToInterrupt)
          Create an interrupter for the specified thread.
 
Method Summary
 void abort()
          Abort the request to interrupt the thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DelayedInterrupter

public DelayedInterrupter(long timeToInterrupt)
Create an interrupter for the current thread.

Parameters:
timeToInterrupt - Number of milliseconds to allow an abort before the thread will be interrupted.

DelayedInterrupter

public DelayedInterrupter(Thread worker,
                          long timeToInterrupt)
Create an interrupter for the specified thread.

Parameters:
timeToInterrupt - Number of milliseconds to allow an abort before the thread will be interrupted.
Method Detail

abort

public void abort()
Abort the request to interrupt the thread. When called from the worker thread (this is the intended usage), clears the interrupted status, in case the interrupt occurred but was not detected.