public class EventTimer extends Object implements Timer
Registry
and custom Selectors
to determine when tasks should be executed.
This is specifically useful when RingBuffer HashWheelTimer is not supported (Android).
A SimpleHashWheelTimer
has two variations for scheduling tasks: schedule(reactor.fn.Consumer,
long,
java.util.concurrent.TimeUnit)
and schedule(reactor.fn.Consumer, long, java.util.concurrent.TimeUnit,
long)
which are for scheduling repeating tasks, and submit(reactor.fn.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.fn.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.
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<Long,? extends Consumer<Long>> |
schedule(Consumer<Long> consumer,
long period,
TimeUnit timeUnit)
Schedule a recurring task.
|
Registration<Long,? extends Consumer<Long>> |
schedule(Consumer<Long> consumer,
long period,
TimeUnit timeUnit,
long delayInMilliseconds)
Schedule a recurring task.
|
Registration<Long,? extends Consumer<Long>> |
submit(Consumer<Long> consumer)
Submit a task for arbitrary execution after the delay of this timer's resolution.
|
Registration<Long,? extends Consumer<Long>> |
submit(Consumer<Long> consumer,
long delay,
TimeUnit timeUnit)
Submit a task for arbitrary execution after the given time delay.
|
public long getResolution()
Timer
getResolution
in interface Timer
public Registration<Long,? extends Consumer<Long>> schedule(Consumer<Long> consumer, long period, TimeUnit timeUnit, long delayInMilliseconds)
Timer
Consumer
will be invoked once every N time units
after the given delay.schedule
in interface Timer
consumer
- the Consumer
to invoke each periodperiod
- the amount of time that should elapse between invocations of the given Consumer
timeUnit
- the unit of time the period
is to be measured indelayInMilliseconds
- a number of milliseconds in which to delay any execution of the given Consumer
Pausable
that can be used to cancel
, pause
or
resume
the given task.public Registration<Long,? extends Consumer<Long>> schedule(Consumer<Long> consumer, long period, TimeUnit timeUnit)
Timer
Consumer
will be invoked immediately, as well as
once
every N time units.schedule
in interface Timer
consumer
- the Consumer
to invoke each periodperiod
- the amount of time that should elapse between invocations of the given Consumer
timeUnit
- the unit of time the period
is to be measured inPausable
that can be used to cancel
, pause
or
resume
the given task.Timer.schedule(reactor.fn.Consumer, long, java.util.concurrent.TimeUnit, long)
public Registration<Long,? extends Consumer<Long>> submit(Consumer<Long> consumer, long delay, TimeUnit timeUnit)
Timer
submit
in interface Timer
consumer
- the Consumer
to invokedelay
- the amount of time that should elapse before invocations of the given Consumer
timeUnit
- the unit of time the period
is to be measured inPausable
that can be used to cancel
, pause
or
resume
the given task.public Registration<Long,? extends Consumer<Long>> submit(Consumer<Long> consumer)
Timer
Copyright © 2016. All rights reserved.