Package io.netty.channel.nio
Class NioEventLoop
- All Implemented Interfaces:
EventLoop
,EventLoopGroup
,EventExecutor
,EventExecutorGroup
,OrderedEventExecutor
,AutoCloseable
,Iterable<EventExecutor>
,Executor
,ExecutorService
,ScheduledExecutorService
SingleThreadEventLoop
implementation which register the Channel
's to a
Selector
and so does the multi-plexing of these in the event loop.-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class io.netty.channel.SingleThreadEventLoop
SingleThreadEventLoop.ChannelsReadOnlyIterator<T extends Channel>
Nested classes/interfaces inherited from class io.netty.util.concurrent.SingleThreadEventExecutor
SingleThreadEventExecutor.NonWakeupRunnable
Nested classes/interfaces inherited from class io.netty.util.concurrent.AbstractEventExecutor
AbstractEventExecutor.LazyRunnable
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final long
private int
private static final int
private static final boolean
private int
private static final InternalLogger
private static final int
private boolean
private final AtomicLong
private static final long
private final SelectorProvider
private SelectedSelectionKeySet
private final IntSupplier
private Selector
The NIOSelector
.private static final int
private final SelectStrategy
private Selector
Fields inherited from class io.netty.channel.SingleThreadEventLoop
DEFAULT_MAX_PENDING_TASKS
-
Constructor Summary
ConstructorsConstructorDescriptionNioEventLoop
(NioEventLoopGroup parent, Executor executor, SelectorProvider selectorProvider, SelectStrategy strategy, RejectedExecutionHandler rejectedExecutionHandler, EventLoopTaskQueueFactory taskQueueFactory, EventLoopTaskQueueFactory tailTaskQueueFactory) -
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
afterScheduledTaskSubmitted
(long deadlineNanos) protected boolean
beforeScheduledTaskSubmitted
(long deadlineNanos) Called from arbitrary non-EventExecutor
threads prior to scheduled task submission.(package private) void
cancel
(SelectionKey key) protected void
cleanup()
Do nothing, sub-classes may overrideprivate void
closeAll()
int
Returns the percentage of the desired amount of time spent for I/O in the event loop.private static void
private static void
invokeChannelUnregistered
(NioTask<SelectableChannel> task, SelectionKey k, Throwable cause) newTaskQueue
(int maxPendingTasks) Create a newQueue
which will holds the tasks to execute.newTaskQueue
(EventLoopTaskQueueFactory queueFactory) newTaskQueue0
(int maxPendingTasks) private NioEventLoop.SelectorTuple
private void
private static void
private void
private void
private void
processSelectedKeysPlain
(Set<SelectionKey> selectedKeys) void
private void
void
register
(SelectableChannel ch, int interestOps, NioTask<?> task) Registers an arbitrarySelectableChannel
, not necessarily created by Netty, to theSelector
of this event loop.private void
register0
(SelectableChannel ch, int interestOps, NioTask<?> task) int
protected void
run()
Run the tasks in theSingleThreadEventExecutor.taskQueue
private int
select
(long deadlineNanos) private void
(package private) int
void
setIoRatio
(int ioRatio) Sets the percentage of the desired amount of time spent for I/O in the event loop.private boolean
unexpectedSelectorWakeup
(int selectCnt) (package private) Selector
protected void
wakeup
(boolean inEventLoop) Methods inherited from class io.netty.channel.SingleThreadEventLoop
afterRunningAllTasks, executeAfterEventLoopIteration, hasTasks, next, parent, pendingTasks, register, register, register
Methods inherited from class io.netty.util.concurrent.SingleThreadEventExecutor
addShutdownHook, addTask, awaitTermination, confirmShutdown, deadlineNanos, delayNanos, execute, inEventLoop, interruptThread, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isShuttingDown, isTerminated, lazyExecute, newTaskQueue, peekTask, pollTask, pollTaskFrom, reject, reject, removeShutdownHook, removeTask, runAllTasks, runAllTasks, runAllTasksFrom, runScheduledAndExecutorTasks, shutdown, shutdownGracefully, takeTask, terminationFuture, threadProperties, updateLastExecutionTime, wakesUpForTask
Methods inherited from class io.netty.util.concurrent.AbstractScheduledEventExecutor
cancelScheduledTasks, deadlineToDelayNanos, getCurrentTimeNanos, hasScheduledTasks, initialNanoTime, nanoTime, nextScheduledTaskDeadlineNanos, nextScheduledTaskNano, pollScheduledTask, pollScheduledTask, schedule, schedule, scheduleAtFixedRate, scheduleWithFixedDelay, validateScheduled
Methods inherited from class io.netty.util.concurrent.AbstractEventExecutor
inEventLoop, iterator, newFailedFuture, newProgressivePromise, newPromise, newSucceededFuture, newTaskFor, newTaskFor, runTask, safeExecute, shutdownGracefully, shutdownNow, submit, submit, submit
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.netty.util.concurrent.EventExecutor
inEventLoop, inEventLoop, newFailedFuture, newProgressivePromise, newPromise, newSucceededFuture
Methods inherited from interface io.netty.util.concurrent.EventExecutorGroup
isShuttingDown, iterator, schedule, schedule, scheduleAtFixedRate, scheduleWithFixedDelay, shutdown, shutdownGracefully, shutdownGracefully, shutdownNow, submit, submit, submit, terminationFuture
Methods inherited from interface java.util.concurrent.ExecutorService
awaitTermination, close, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isTerminated
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
logger
-
CLEANUP_INTERVAL
private static final int CLEANUP_INTERVAL- See Also:
-
DISABLE_KEY_SET_OPTIMIZATION
private static final boolean DISABLE_KEY_SET_OPTIMIZATION -
MIN_PREMATURE_SELECTOR_RETURNS
private static final int MIN_PREMATURE_SELECTOR_RETURNS- See Also:
-
SELECTOR_AUTO_REBUILD_THRESHOLD
private static final int SELECTOR_AUTO_REBUILD_THRESHOLD -
selectNowSupplier
-
selector
The NIOSelector
. -
unwrappedSelector
-
selectedKeys
-
provider
-
AWAKE
private static final long AWAKE- See Also:
-
NONE
private static final long NONE- See Also:
-
nextWakeupNanos
-
selectStrategy
-
ioRatio
private volatile int ioRatio -
cancelledKeys
private int cancelledKeys -
needsToSelectAgain
private boolean needsToSelectAgain
-
-
Constructor Details
-
NioEventLoop
NioEventLoop(NioEventLoopGroup parent, Executor executor, SelectorProvider selectorProvider, SelectStrategy strategy, RejectedExecutionHandler rejectedExecutionHandler, EventLoopTaskQueueFactory taskQueueFactory, EventLoopTaskQueueFactory tailTaskQueueFactory)
-
-
Method Details
-
newTaskQueue
-
openSelector
-
selectorProvider
-
newTaskQueue
Description copied from class:SingleThreadEventExecutor
Create a newQueue
which will holds the tasks to execute. This default implementation will return aLinkedBlockingQueue
but if your sub-class ofSingleThreadEventExecutor
will not do any blocking calls on the thisQueue
it may make sense to@Override
this and return some more performant implementation that does not support blocking operations at all.- Overrides:
newTaskQueue
in classSingleThreadEventExecutor
-
newTaskQueue0
-
register
Registers an arbitrarySelectableChannel
, not necessarily created by Netty, to theSelector
of this event loop. Once the specifiedSelectableChannel
is registered, the specifiedtask
will be executed by this event loop when theSelectableChannel
is ready. -
register0
-
getIoRatio
public int getIoRatio()Returns the percentage of the desired amount of time spent for I/O in the event loop. -
setIoRatio
public void setIoRatio(int ioRatio) Sets the percentage of the desired amount of time spent for I/O in the event loop. Value range from 1-100. The default value is50
, which means the event loop will try to spend the same amount of time for I/O as for non-I/O tasks. The lower the number the more time can be spent on non-I/O tasks. If value set to100
, this feature will be disabled and event loop will not attempt to balance I/O and non-I/O tasks. -
rebuildSelector
public void rebuildSelector() -
registeredChannels
public int registeredChannels()Description copied from class:SingleThreadEventLoop
Returns the number ofChannel
s registered with thisEventLoop
or-1
if operation is not supported. The returned value is not guaranteed to be exact accurate and should be viewed as a best effort.- Overrides:
registeredChannels
in classSingleThreadEventLoop
-
registeredChannelsIterator
- Overrides:
registeredChannelsIterator
in classSingleThreadEventLoop
- Returns:
- read-only iterator of active
Channel
s registered with thisEventLoop
. The returned value is not guaranteed to be exact accurate and should be viewed as a best effort. This method is expected to be called from within event loop.
-
rebuildSelector0
private void rebuildSelector0() -
run
protected void run()Description copied from class:SingleThreadEventExecutor
Run the tasks in theSingleThreadEventExecutor.taskQueue
- Specified by:
run
in classSingleThreadEventExecutor
-
unexpectedSelectorWakeup
private boolean unexpectedSelectorWakeup(int selectCnt) -
handleLoopException
-
processSelectedKeys
private void processSelectedKeys() -
cleanup
protected void cleanup()Description copied from class:SingleThreadEventExecutor
Do nothing, sub-classes may override- Overrides:
cleanup
in classSingleThreadEventExecutor
-
cancel
-
processSelectedKeysPlain
-
processSelectedKeysOptimized
private void processSelectedKeysOptimized() -
processSelectedKey
-
processSelectedKey
-
closeAll
private void closeAll() -
invokeChannelUnregistered
private static void invokeChannelUnregistered(NioTask<SelectableChannel> task, SelectionKey k, Throwable cause) -
wakeup
protected void wakeup(boolean inEventLoop) - Overrides:
wakeup
in classSingleThreadEventExecutor
-
beforeScheduledTaskSubmitted
protected boolean beforeScheduledTaskSubmitted(long deadlineNanos) Description copied from class:AbstractScheduledEventExecutor
Called from arbitrary non-EventExecutor
threads prior to scheduled task submission. Returnstrue
if theEventExecutor
thread should be woken immediately to process the scheduled task (if not already awake).If
false
is returned,AbstractScheduledEventExecutor.afterScheduledTaskSubmitted(long)
will be called with the same value after the scheduled task is enqueued, providing another opportunity to wake theEventExecutor
thread if required.- Overrides:
beforeScheduledTaskSubmitted
in classAbstractScheduledEventExecutor
- Parameters:
deadlineNanos
- deadline of the to-be-scheduled task relative toAbstractScheduledEventExecutor.getCurrentTimeNanos()
- Returns:
true
if theEventExecutor
thread should be woken,false
otherwise
-
afterScheduledTaskSubmitted
protected boolean afterScheduledTaskSubmitted(long deadlineNanos) Description copied from class:AbstractScheduledEventExecutor
SeeAbstractScheduledEventExecutor.beforeScheduledTaskSubmitted(long)
. Called only after that method returns false.- Overrides:
afterScheduledTaskSubmitted
in classAbstractScheduledEventExecutor
- Parameters:
deadlineNanos
- relative toAbstractScheduledEventExecutor.getCurrentTimeNanos()
- Returns:
true
if theEventExecutor
thread should be woken,false
otherwise
-
unwrappedSelector
Selector unwrappedSelector() -
selectNow
- Throws:
IOException
-
select
- Throws:
IOException
-
selectAgain
private void selectAgain()
-