public class SimpleHashWheelTimer extends java.lang.Object implements Timer
Registry and custom Selectors to determine when tasks should be executed.
A SimpleHashWheelTimer has two variations for scheduling tasks: schedule(reactor.function.Consumer,
long,
java.util.concurrent.TimeUnit) and schedule(reactor.function.Consumer, long, java.util.concurrent.TimeUnit,
long) which are for scheduling repeating tasks, and submit(reactor.function.Consumer, long,
java.util.concurrent.TimeUnit) which is for scheduling single-run delayed tasks.
To schedule a repeating task, specify the period of time which should elapse before invoking the given Consumer. To schedule a task that repeats every 5 seconds, for example, one would do something
like:
SimpleHashWheelTimer timer = new SimpleHashWheelTimer();
timer.schedule(new Consumer<Long>() {
public void accept(Long now) {
// run a task
}
}, 5, TimeUnit.SECONDS);
NOTE: Without delaying a task, it will be run immediately, in addition to being run after the elapsed time has
expired. To run a task only once every N time units and not immediately, use the schedule(reactor.function.Consumer, long, java.util.concurrent.TimeUnit, long) method, which allows you to specify
an additional delay that must expire before the task will be executed.
| Constructor and Description |
|---|
SimpleHashWheelTimer()
Create a new
SimpleHashWheelTimer using the default resolution of 50ms. |
SimpleHashWheelTimer(int resolution)
Create a new
SimpleHashWheelTimer using the given timer resolution. |
| Modifier and Type | Method and Description |
|---|---|
void |
cancel()
Cancel this timer by interrupting the task thread.
|
long |
getResolution()
Get the resolution of this tTimer.
|
Registration<? extends Consumer<java.lang.Long>> |
schedule(Consumer<java.lang.Long> consumer,
long period,
java.util.concurrent.TimeUnit timeUnit)
Schedule a recurring task.
|
Registration<? extends Consumer<java.lang.Long>> |
schedule(Consumer<java.lang.Long> consumer,
long period,
java.util.concurrent.TimeUnit timeUnit,
long delayInMilliseconds)
Schedule a recurring task.
|
Registration<? extends Consumer<java.lang.Long>> |
submit(Consumer<java.lang.Long> consumer)
Submit a task for arbitrary execution after the delay of this timer's resolution.
|
Registration<? extends Consumer<java.lang.Long>> |
submit(Consumer<java.lang.Long> consumer,
long delay,
java.util.concurrent.TimeUnit timeUnit)
Submit a task for arbitrary execution after the given time delay.
|
public SimpleHashWheelTimer()
SimpleHashWheelTimer using the default resolution of 50ms.public SimpleHashWheelTimer(int resolution)
SimpleHashWheelTimer using the given timer resolution. All times will rounded up to the
closest
multiple of this resolution.resolution - the resolution of this timer, in millisecondspublic long getResolution()
TimergetResolution in interface Timerpublic Registration<? extends Consumer<java.lang.Long>> schedule(Consumer<java.lang.Long> consumer, long period, java.util.concurrent.TimeUnit timeUnit, long delayInMilliseconds)
TimerConsumer will be invoked once every N time units
after the given delay.schedule in interface Timerconsumer - the Consumer to invoke each periodperiod - the amount of time that should elapse between invocations of the given ConsumertimeUnit - the unit of time the period is to be measured indelayInMilliseconds - a number of milliseconds in which to delay any execution of the given ConsumerRegistration that can be used to cancel, pause or
resume the given task.public Registration<? extends Consumer<java.lang.Long>> schedule(Consumer<java.lang.Long> consumer, long period, java.util.concurrent.TimeUnit timeUnit)
TimerConsumer will be invoked immediately, as well as
once
every N time units.schedule in interface Timerconsumer - the Consumer to invoke each periodperiod - the amount of time that should elapse between invocations of the given ConsumertimeUnit - the unit of time the period is to be measured inRegistration that can be used to cancel, pause or
resume the given task.Timer.schedule(reactor.function.Consumer, long, java.util.concurrent.TimeUnit, long)public Registration<? extends Consumer<java.lang.Long>> submit(Consumer<java.lang.Long> consumer, long delay, java.util.concurrent.TimeUnit timeUnit)
Timersubmit in interface Timerconsumer - the Consumer to invokedelay - the amount of time that should elapse before invocations of the given ConsumertimeUnit - the unit of time the period is to be measured inRegistration that can be used to cancel, pause or
resume the given task.public Registration<? extends Consumer<java.lang.Long>> submit(Consumer<java.lang.Long> consumer)
Timer