An abstract implementation of a timeout.
Subclasses should implement onTimeoutExpired().
This implementation is optimised assuming that the timeout will mostly be cancelled and then reused with a similar value.
The typical scenario to use this class is when you have events that postpone (by re-scheduling), or cancel then re-schedule, a timeout for a single entity. For example: connection idleness, where for each connection there is a CyclicTimeout and a read/write postpones the timeout; when the timeout expires, the implementation checks against a timestamp if the connection is really idle. Another example: HTTP session expiration, where for each HTTP session there is a CyclicTimeout and at the beginning of the request processing the timeout is canceled (via cancel()), but at the end of the request processing the timeout is re-scheduled.
This implementation has a Timeout holding the time
at which the scheduled task should fire, and a linked list of
Wakeup, each holding the actual scheduled task.
Calling schedule(long, TimeUnit) the first time will
create a Timeout with an associated Wakeup and submit a task to
the scheduler.
Calling schedule(long, TimeUnit) again with the same or
a larger delay will cancel the previous Timeout, but keep the
previous Wakeup without submitting a new task to the scheduler,
therefore reducing the pressure on the scheduler and avoid it
becomes a bottleneck.
When the Wakeup task fires, it will see that the Timeout is now
in the future and will attach a new Wakeup with the future time
to the Timeout, and submit a scheduler task for the new Wakeup.
CyclicTimeouts
| Modifier and Type | Class and Description |
|---|---|
| private static class | CyclicTimeout.Timeout
A timeout time with a link to a Wakeup chain. |
| private class | CyclicTimeout.Wakeup
A Wakeup chain of real scheduler tasks. |
| Modifier and Type | Field and Description |
|---|---|
| private final Scheduler | |
| private final AtomicReference | |
| private static final Scheduler. | |
| private static final Logger | |
| private static final CyclicTimeout. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | Returns: true if this CyclicTimeout was scheduled to expireCancels this CyclicTimeout so that it won't expire. |
| public void | destroy()
Implements org. Destroys this CyclicTimeout. |
| public Scheduler | |
| public abstract void | |
| public boolean |
| _scheduler | back to summary |
|---|---|
| private final Scheduler _scheduler | |
| _timeout | back to summary |
|---|---|
| private final AtomicReference<CyclicTimeout. | |
| DESTROYED | back to summary |
|---|---|
| private static final Scheduler. | |
| LOG | back to summary |
|---|---|
| private static final Logger LOG | |
| NOT_SET | back to summary |
|---|---|
| private static final CyclicTimeout. | |
| CyclicTimeout | back to summary |
|---|---|
public CyclicTimeout(Scheduler scheduler)
| |
| cancel | back to summary |
|---|---|
| public boolean cancel() Cancels this CyclicTimeout so that it won't expire. After being cancelled, this CyclicTimeout can be scheduled again.
| |
| destroy | back to summary |
|---|---|
| public void destroy() Implements org. Destroys this CyclicTimeout. After being destroyed, this CyclicTimeout is not used anymore.
| |
| getScheduler | back to summary |
|---|---|
| public Scheduler getScheduler() | |
| onTimeoutExpired | back to summary |
|---|---|
| public abstract void onTimeoutExpired() Invoked when the timeout expires. | |
| schedule | back to summary |
|---|---|
| public boolean schedule(long delay, TimeUnit units) Schedules a timeout, even if already set, cancelled or expired. If a timeout is already set, it will be cancelled and replaced by the new one.
| |
| Modifier and Type | Field and Description |
|---|---|
| private final long | |
| private final CyclicTimeout. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public String |
| _at | back to summary |
|---|---|
| private final long _at | |
| _wakeup | back to summary |
|---|---|
| private final CyclicTimeout. | |
| Timeout | back to summary |
|---|---|
| private Timeout(long timeoutAt, CyclicTimeout. | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
| Modifier and Type | Field and Description |
|---|---|
| private final long | |
| private final CyclicTimeout. | |
| private final AtomicReference |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| public void | |
| private void | |
| public String |
| _at | back to summary |
|---|---|
| private final long _at | |
| _next | back to summary |
|---|---|
| private final CyclicTimeout. | |
| _task | back to summary |
|---|---|
| private final AtomicReference<Scheduler. | |
| Wakeup | back to summary |
|---|---|
| private Wakeup(long wakeupAt, CyclicTimeout. | |
| destroy | back to summary |
|---|---|
| private void destroy() | |
| run | back to summary |
|---|---|
| public void run() Implements java. Doc from java. Runs this operation.
| |
| schedule | back to summary |
|---|---|
| private void schedule(long now) | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |