public class Timeout extends Object implements AutoCloseable
try (Timeout t = Timeout.after(5, TimeUnit.SECONDS))
{
doSomething();
} // this thread will self-interrupt if it doesn't reach this line within 5 seconds
catch (InterruptedException e)
{
// thread timed out or was otherwise interrupted
}
In order for this to work any blocking code executed from within the context of the Timeout must use the Blocking static helper methods for blocking. An example of a compatible blocking call would be:
void doSomething()
{
Object oField = m_oField;
synchronized (oField)
{
Blocking.wait(oField); // rather then oField.wait();
}
}
Note that Timeout can only self-interrupt at interruptible points, and does not defend against CPU bound loops for example.| Modifier and Type | Field and Description |
|---|---|
protected long |
f_cMillisTimeout
This Timeout's timeout.
|
protected boolean |
f_fTloCreator
True iff this Timeout created (and thus must ultimately destroy) the TLO.
|
protected long |
f_lTimeoutOrig
The original timeout before this instance changed it.
|
protected MutableLong |
f_mlTimeout
Cached reference to the thread's MutableLong holding it's current timeout.
|
protected static ThreadLocal<MutableLong> |
s_tloTimeout
A thread-local containing the calling thread's timeout value.
|
| Modifier | Constructor and Description |
|---|---|
protected |
Timeout(long cMillis, boolean fForceOverride)
Specify a new timeout.
|
| Modifier and Type | Method and Description |
|---|---|
static Timeout |
after(long cMillis)
Specify a new timeout.
|
static Timeout |
after(long time, TimeUnit unit)
Specify a new timeout.
|
void |
close()
As part of closing the Timeout resource any former timeout will be restored.
|
static boolean |
isTimedOut()
Return true if the calling thread is timed out.
|
static Timeout |
override(long cMillis)
Specify a new timeout, potentially extending an already active timeout.
|
static long |
remainingTimeoutMillis()
Return the number of milliseconds before this thread will timeout.
|
protected final boolean f_fTloCreator
protected final MutableLong f_mlTimeout
protected final long f_cMillisTimeout
protected final long f_lTimeoutOrig
protected static final ThreadLocal<MutableLong> s_tloTimeout
protected Timeout(long cMillis,
boolean fForceOverride)
cMillis - the new timeout.fForceOverride - true if this timeout is allowed to extend a parent timeout.
public void close()
throws InterruptedException
close in interface AutoCloseableInterruptedException - if the calling thread is interruptedpublic static Timeout after(long time, TimeUnit unit)
time - the new timeoutunit - the unit the timeout is expressed inpublic static Timeout after(long cMillis)
cMillis - the new timeoutpublic static Timeout override(long cMillis)
This variant allows the caller to extend a parent timeout. This is rarely needed, and is roughly the equivalent of silently consuming a thread interrupt without rethrowing the InterruptedException. Use of this method should be extremely limited.
cMillis - the new timeoutpublic static long remainingTimeoutMillis()
public static boolean isTimedOut()