protected static final class CancellableTimer.ScheduledEventHandler extends Object implements EventHandler, Closeable
Modifier and Type | Method and Description |
---|---|
boolean |
action()
Performs an action corresponding to some amount of work and returns if it is expected that a
subsequent call to this method would result in additional work being carried out.
|
void |
close() |
@NotNull HandlerPriority |
priority() |
String |
toString() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
eventLoop, loopFinished, loopStarted
public boolean action() throws InvalidEventHandlerException
VanillaEventHandler
This method is called from an event loop's execution thread. Each event loop can services multiple event handlers. The aggressiveness with which any one handler is serviced is influenced by the handler's priority as well as other activity on the event loop as a whole.
If an event handler returns true
from action(), it biases the event loop to service the same
handler again "soon". How soon depends on a variety of factors and the other work the event loop has
to do across the other handlers.
Returning true
when there is no actual work to do may waste cycles servicing a handler which has nothing
to do, at the expense of stealing cycles away from other handlers.
Conversely, returning false
when there is work to do will effectively increase latency as the event loop
will take the "false" as a hint that several other handlers can be serviced ahead of this one.
As a rule of thumb, an action handler should do a certain amount of work then yield/return
If it knows for sure that there is remaining work to be done at the point of yielding then return true
.
Otherwise return false
and the event loop will revisit based on the handler's priority and other work load.
As with a lot of scheduling approaches there's no single answer and some experimentation under typical loads would always be recommended. But the above rule of thumb is a good starting point.
When the event handler is not required anymore and should be removed from the event loop, the
InvalidEventHandlerException.reusable()
method returns a reusable pre-created
InvalidEventHandlerException which can be thrown to remove the EventHandler from the EventLoop.
action
in interface VanillaEventHandler
InvalidEventHandlerException
- when the event handler is not required anymore and should be removed from
the event loop.
It is recommended to throw this exception if the event handler is closed.
The InvalidEventHandlerException.reusable() method returns a reusable, pre-created,
InvalidEventHandlerException that is unmodifiable and contains no stack trace.
See InvalidEventHandlerException.reusable()
.@NotNull public @NotNull HandlerPriority priority()
priority
in interface EventHandler
public void close()
close
in interface Closeable
close
in interface AutoCloseable
Copyright © 2023. All rights reserved.