public final class ZTicket
extends java.lang.Object
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.
Modifier and Type | Class and Description |
---|---|
static class |
ZTicket.Ticket
Opaque representation of a ticket.
|
Modifier and Type | Field and Description |
---|---|
private Supplier<java.lang.Long> |
clock |
private boolean |
sort |
private java.util.List<ZTicket.Ticket> |
tickets |
Constructor and Description |
---|
ZTicket() |
ZTicket(Supplier<java.lang.Long> clock) |
ZTicket(Supplier<java.lang.Long> clock,
java.util.List<ZTicket.Ticket> tickets) |
Modifier and Type | Method and 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.
|
private final java.util.List<ZTicket.Ticket> tickets
private final Supplier<java.lang.Long> clock
private boolean sort
public ZTicket()
ZTicket(Supplier<java.lang.Long> clock)
ZTicket(Supplier<java.lang.Long> clock, java.util.List<ZTicket.Ticket> tickets)
private long now()
private void insert(ZTicket.Ticket ticket)
public ZTicket.Ticket add(long delay, TimerHandler handler, java.lang.Object... args)
delay
- the expiration delay in milliseconds.handler
- the callback called at the expiration of the ticket.args
- the optional arguments for the handler.public long timeout()
public int execute()
private void sortIfNeeded()