Package org.zeromq.timer
Class ZTicket
- java.lang.Object
-
- org.zeromq.timer.ZTicket
-
public final class ZTicket extends java.lang.Object
Manages set of tickets.Ticket timers are very fast in the case where you use a lot of timers (thousands), and frequently remove and add them. The main use case is expiry timers for servers that handle many clients, and which reset the expiry timer for each message received from a client. Whereas normal timers perform poorly as the number of clients grows, the cost of ticket timers is constant, no matter the number of clients
Tickets can be added with a given delay.
When the delay of time expires after addition, handler method is executed with given arguments.
Ticket is NOT repetitive and will be executed once unless canceled.This class is not thread-safe
This is a DRAFT class, and may change without notice.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ZTicket.Ticket
Opaque representation of a ticket.
-
Field Summary
Fields Modifier and Type Field Description private Supplier<java.lang.Long>
clock
private boolean
sort
private java.util.List<ZTicket.Ticket>
tickets
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ZTicket.Ticket
add(long delay, TimerHandler handler, java.lang.Object... args)
Add ticket to the set.int
execute()
Execute the tickets.private void
insert(ZTicket.Ticket ticket)
private long
now()
private void
sortIfNeeded()
long
timeout()
Returns the time in millisecond until the next ticket.
-
-
-
Field Detail
-
tickets
private final java.util.List<ZTicket.Ticket> tickets
-
clock
private final Supplier<java.lang.Long> clock
-
sort
private boolean sort
-
-
Constructor Detail
-
ZTicket
public ZTicket()
-
ZTicket
ZTicket(Supplier<java.lang.Long> clock)
-
ZTicket
ZTicket(Supplier<java.lang.Long> clock, java.util.List<ZTicket.Ticket> tickets)
-
-
Method Detail
-
now
private long now()
-
insert
private void insert(ZTicket.Ticket ticket)
-
add
public ZTicket.Ticket add(long delay, TimerHandler handler, java.lang.Object... args)
Add ticket to the set.- Parameters:
delay
- the expiration delay in milliseconds.handler
- the callback called at the expiration of the ticket.args
- the optional arguments for the handler.- Returns:
- an opaque handle for further cancel and reset.
-
timeout
public long timeout()
Returns the time in millisecond until the next ticket.- Returns:
- the time in millisecond until the next ticket.
-
execute
public int execute()
Execute the tickets.- Returns:
- the number of tickets triggered.
-
sortIfNeeded
private void sortIfNeeded()
-
-