Class CyclicTimeout
- All Implemented Interfaces:
Destroyable
- Direct Known Subclasses:
HttpDestination.TimeoutTask
,TimeoutCompleteListener
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.
This implementation has a CyclicTimeout.Timeout
holding the time
at which the scheduled task should fire, and a linked list of
CyclicTimeout.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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
A timeout time with a link to a Wakeup chain.private class
A Wakeup chain of real scheduler tasks. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Scheduler
private final AtomicReference
<CyclicTimeout.Timeout> private static final Scheduler.Task
private static final Logger
private static final CyclicTimeout.Timeout
-
Constructor Summary
Constructors -
Method Summary
-
Field Details
-
LOG
-
NOT_SET
-
DESTROYED
-
_scheduler
-
_timeout
-
-
Constructor Details
-
CyclicTimeout
- Parameters:
scheduler
- A scheduler used to schedule wakeups
-
-
Method Details
-
getScheduler
-
schedule
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.
- Parameters:
delay
- The period of time before the timeout expires.units
- The unit of time of the period.- Returns:
- true if the timeout was already set.
-
cancel
public boolean cancel()Cancels this CyclicTimeout so that it won't expire.
After being cancelled, this CyclicTimeout can be scheduled again.
- Returns:
- true if this CyclicTimeout was scheduled to expire
- See Also:
-
onTimeoutExpired
public abstract void onTimeoutExpired()Invoked when the timeout expires.
-
destroy
public void destroy()Destroys this CyclicTimeout.
After being destroyed, this CyclicTimeout is not used anymore.
- Specified by:
destroy
in interfaceDestroyable
-