Package zmq.util
Class Timers
- java.lang.Object
-
- zmq.util.Timers
-
public final class Timers extends java.lang.Object
Manages set of timers. Timers can be added with a given interval, when the interval of time expires after addition, handler method is executed with given arguments. Timer is repetitive and will be executed over time until canceled. This is a DRAFT class, and may change without notice.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
Timers.Handler
static class
Timers.Timer
-
Field Summary
Fields Modifier and Type Field Description private Supplier<java.lang.Long>
clock
private MultiMap<java.lang.Long,Timers.Timer>
timers
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Timers.Timer
add(long interval, Timers.Handler handler, java.lang.Object... args)
Add timer to the set, timer repeats forever, or until cancel is called.boolean
cancel(Timers.Timer timer)
Deprecated.useTimers.Timer.cancel()
instead(package private) java.lang.Iterable<java.util.Map.Entry<Timers.Timer,java.lang.Long>>
entries()
int
execute()
Execute the timers.private boolean
insert(Timers.Timer timer)
private long
now()
boolean
reset(Timers.Timer timer)
Deprecated.useTimers.Timer.reset()
insteadboolean
setInterval(Timers.Timer timer, long interval)
Deprecated.useTimers.Timer.setInterval(long)
insteadint
sleepAndExecute()
long
timeout()
Returns the time in millisecond until the next timer.
-
-
-
Field Detail
-
timers
private final MultiMap<java.lang.Long,Timers.Timer> timers
-
clock
private final Supplier<java.lang.Long> clock
-
-
Constructor Detail
-
Timers
public Timers()
-
Timers
public Timers(Supplier<java.lang.Long> clock)
Builds a new timer.This constructor is for testing and is not intended to be used in production code.
- Parameters:
clock
- the supplier of the current time in milliseconds.
-
-
Method Detail
-
now
private long now()
-
insert
private boolean insert(Timers.Timer timer)
-
add
public Timers.Timer add(long interval, Timers.Handler handler, java.lang.Object... args)
Add timer to the set, timer repeats forever, or until cancel is called.- Parameters:
interval
- the interval of repetition in milliseconds.handler
- the callback called at the expiration of the timer.args
- the optional arguments for the handler.- Returns:
- an opaque handle for further cancel.
-
setInterval
@Deprecated public boolean setInterval(Timers.Timer timer, long interval)
Deprecated.useTimers.Timer.setInterval(long)
insteadChanges the interval of the timer. This method is slow, canceling existing and adding a new timer yield better performance.- Parameters:
timer
- the timer to change the interval to.- Returns:
- true if set, otherwise false.
-
reset
@Deprecated public boolean reset(Timers.Timer timer)
Deprecated.useTimers.Timer.reset()
insteadReset the timer. This method is slow, canceling existing and adding a new timer yield better performance.- Parameters:
timer
- the timer to reset.- Returns:
- true if reset, otherwise false.
-
cancel
@Deprecated public boolean cancel(Timers.Timer timer)
Deprecated.useTimers.Timer.cancel()
insteadCancel a timer.- Parameters:
timer
- the timer to cancel.- Returns:
- true if cancelled, otherwise false.
-
timeout
public long timeout()
Returns the time in millisecond until the next timer.- Returns:
- the time in millisecond until the next timer.
-
execute
public int execute()
Execute the timers.- Returns:
- the number of timers triggered.
-
entries
java.lang.Iterable<java.util.Map.Entry<Timers.Timer,java.lang.Long>> entries()
-
sleepAndExecute
public int sleepAndExecute()
-
-