public abstract class SelectLoop extends Object implements Runnable
Modifier and Type | Field and Description |
---|---|
static long |
DEFAULT_TIMEOUT
Default select timeout of 500L milliseconds.
|
protected org.slf4j.Logger |
log
Shared logger.
|
protected Selector |
selector
Shared selector used to drive the loop operation.
|
protected boolean |
stopped
When true, implementations are expected to break their loop.
|
protected long |
timeout
Select timeout in milliseconds.
|
Constructor and Description |
---|
SelectLoop()
Creates a select loop with default timeout.
|
SelectLoop(long timeout)
Creates a select loop with the specified timeout.
|
Modifier and Type | Method and Description |
---|---|
void |
cease()
Gracefully stops the loop.
|
Throwable |
getError()
Gets the prior I/O error, if one occurred.
|
protected abstract void |
loop()
I/O event loop.
|
protected Selector |
openSelector()
Opens a new selector for the use by the loop.
|
void |
run() |
protected void |
signalStart()
Signals all observers that loop has started.
|
boolean |
waitForFinish(long timeout)
Waits for the loop execution to stop.
|
boolean |
waitForStart(long timeout)
Waits for the loop execution to start.
|
protected org.slf4j.Logger log
public static final long DEFAULT_TIMEOUT
protected Selector selector
protected volatile boolean stopped
protected long timeout
public SelectLoop() throws IOException
IOException
- if unable to open selectorpublic SelectLoop(long timeout) throws IOException
timeout
- select timeout in millisecondsIOException
- if unable to open selectorprotected Selector openSelector() throws IOException
IOException
- if unable to open selectorpublic void cease()
protected abstract void loop() throws IOException
stopped
becomes true
via cease()
invocation.
Also, implementations are expected to signalStart()
when
ready.IOException
- if unable to properly select channels or an I/O
error is propagated by an event handler.public void run()
This implementation first resets the stopped state to false, then
allocates the selector and finally invokes the loop()
method.
protected void signalStart()
Implementations are expected to invoke this method from their
loop()
method when ready to start processing requests.
public final boolean waitForStart(long timeout)
timeout
- number of milliseconds to waitpublic final boolean waitForFinish(long timeout)
timeout
- number of milliseconds to waitpublic Throwable getError()
Copyright © 2015. All Rights Reserved.