- All Implemented Interfaces:
- Direct Known Subclasses:
public abstract class CyclicTimeout extends java.lang.Object implements Destroyable
An abstract implementation of a timeout.
Subclasses should implement
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
CyclicTimeout.Timeoutholding the time at which the scheduled task should fire, and a linked list of
CyclicTimeout.Wakeup, each holding the actual scheduled task.
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.
- See Also:
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description
cancel()Cancels this CyclicTimeout so that it won't expire.
destroy()Destroys this CyclicTimeout.
onTimeoutExpired()Invoked when the timeout expires.
schedule(long delay, java.util.concurrent.TimeUnit units)Schedules a timeout, even if already set, cancelled or expired.
public CyclicTimeout(Scheduler scheduler)
scheduler- A scheduler used to schedule wakeups
public Scheduler getScheduler()
public boolean schedule(long delay, java.util.concurrent.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.
delay- The period of time before the timeout expires.
units- The unit of time of the period.
- true if the timeout was already set.
public boolean cancel()
Cancels this CyclicTimeout so that it won't expire.
After being cancelled, this CyclicTimeout can be scheduled again.
- true if this CyclicTimeout was scheduled to expire
- See Also:
public abstract void onTimeoutExpired()
Invoked when the timeout expires.