public abstract class WakeableLooper
extends java.lang.Object
The WakeableLooper will execute in a while loop unless the exitLoop() is called. And in every
execution, it will execute runOnce(), which will:
1. doWait(), which in fact is implemented by selector.select(timeout), and it will be wake up if other threads
wake it up, it meets the timeout, one channel is selected, or the current thread is interrupted.
2. run executeTasksOnWakeup(), which is a list of Runnable, e.g. tasks added to run every time. We could add tasks
even during executionTasksOnWakeup, but the task added will be executed next time we run executeTasksOnwakeup().
Notice: you could just add tasks into it but not remove tasks from it.
3. trigger the timers, which is a priority queue of TimerTask
, the TimerTask
will be removed after execution.
So to use this class, user could add the persistent tasks, one time tasks and timer tasks as many as they want.
Constructor and Description |
---|
WakeableLooper() |
Modifier and Type | Method and Description |
---|---|
void |
addTasksOnExit(java.lang.Runnable task) |
void |
addTasksOnWakeup(java.lang.Runnable task) |
protected abstract void |
doWait() |
void |
exitLoop() |
protected long |
getNextTimeoutIntervalMs()
Get the timeout in milli-seconds which should be used in doWait().
|
void |
loop() |
void |
registerTimerEventInNanoSeconds(long timerInNanoSecnods,
java.lang.Runnable task) |
void |
registerTimerEventInSeconds(long timerInSeconds,
java.lang.Runnable task) |
abstract void |
wakeUp() |
public void loop()
protected abstract void doWait()
public abstract void wakeUp()
public void addTasksOnWakeup(java.lang.Runnable task)
public void addTasksOnExit(java.lang.Runnable task)
public void registerTimerEventInSeconds(long timerInSeconds, java.lang.Runnable task)
public void registerTimerEventInNanoSeconds(long timerInNanoSecnods, java.lang.Runnable task)
public void exitLoop()
protected long getNextTimeoutIntervalMs()