Package io.netty.channel.nio
Class NioEventLoop
- java.lang.Object
-
- java.util.concurrent.AbstractExecutorService
-
- All Implemented Interfaces:
EventLoop,EventLoopGroup,EventExecutor,EventExecutorGroup,OrderedEventExecutor,java.lang.Iterable<EventExecutor>,java.util.concurrent.Executor,java.util.concurrent.ExecutorService,java.util.concurrent.ScheduledExecutorService
public final class NioEventLoop extends SingleThreadEventLoop
SingleThreadEventLoopimplementation which register theChannel's to aSelectorand so does the multi-plexing of these in the event loop.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classNioEventLoop.SelectorTuple-
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
Fields Modifier and Type Field Description private static longAWAKEprivate intcancelledKeysprivate static intCLEANUP_INTERVALprivate static booleanDISABLE_KEY_SET_OPTIMIZATIONprivate intioRatioprivate static InternalLoggerloggerprivate static intMIN_PREMATURE_SELECTOR_RETURNSprivate booleanneedsToSelectAgainprivate java.util.concurrent.atomic.AtomicLongnextWakeupNanosprivate static longNONEprivate java.nio.channels.spi.SelectorProviderproviderprivate SelectedSelectionKeySetselectedKeysprivate IntSupplierselectNowSupplierprivate java.nio.channels.SelectorselectorThe NIOSelector.private static intSELECTOR_AUTO_REBUILD_THRESHOLDprivate SelectStrategyselectStrategyprivate java.nio.channels.SelectorunwrappedSelector-
Fields inherited from class io.netty.channel.SingleThreadEventLoop
DEFAULT_MAX_PENDING_TASKS
-
-
Constructor Summary
Constructors Constructor Description NioEventLoop(NioEventLoopGroup parent, java.util.concurrent.Executor executor, java.nio.channels.spi.SelectorProvider selectorProvider, SelectStrategy strategy, RejectedExecutionHandler rejectedExecutionHandler, EventLoopTaskQueueFactory queueFactory)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected booleanafterScheduledTaskSubmitted(long deadlineNanos)protected booleanbeforeScheduledTaskSubmitted(long deadlineNanos)Called from arbitrary non-EventExecutorthreads prior to scheduled task submission.(package private) voidcancel(java.nio.channels.SelectionKey key)protected voidcleanup()Do nothing, sub-classes may overrideprivate voidcloseAll()intgetIoRatio()Returns the percentage of the desired amount of time spent for I/O in the event loop.private static voidhandleLoopException(java.lang.Throwable t)private static voidinvokeChannelUnregistered(NioTask<java.nio.channels.SelectableChannel> task, java.nio.channels.SelectionKey k, java.lang.Throwable cause)protected java.util.Queue<java.lang.Runnable>newTaskQueue(int maxPendingTasks)Create a newQueuewhich will holds the tasks to execute.private static java.util.Queue<java.lang.Runnable>newTaskQueue(EventLoopTaskQueueFactory queueFactory)private static java.util.Queue<java.lang.Runnable>newTaskQueue0(int maxPendingTasks)private NioEventLoop.SelectorTupleopenSelector()private voidprocessSelectedKey(java.nio.channels.SelectionKey k, AbstractNioChannel ch)private static voidprocessSelectedKey(java.nio.channels.SelectionKey k, NioTask<java.nio.channels.SelectableChannel> task)private voidprocessSelectedKeys()private voidprocessSelectedKeysOptimized()private voidprocessSelectedKeysPlain(java.util.Set<java.nio.channels.SelectionKey> selectedKeys)voidrebuildSelector()Replaces the currentSelectorof this event loop with newly createdSelectors to work around the infamous epoll 100% CPU bug.private voidrebuildSelector0()voidregister(java.nio.channels.SelectableChannel ch, int interestOps, NioTask<?> task)Registers an arbitrarySelectableChannel, not necessarily created by Netty, to theSelectorof this event loop.private voidregister0(java.nio.channels.SelectableChannel ch, int interestOps, NioTask<?> task)intregisteredChannels()protected voidrun()Run the tasks in theSingleThreadEventExecutor.taskQueueprivate intselect(long deadlineNanos)private voidselectAgain()(package private) intselectNow()java.nio.channels.spi.SelectorProviderselectorProvider()voidsetIoRatio(int ioRatio)Sets the percentage of the desired amount of time spent for I/O in the event loop.private booleanunexpectedSelectorWakeup(int selectCnt)(package private) java.nio.channels.SelectorunwrappedSelector()protected voidwakeup(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, 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, 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
-
-
-
-
Field Detail
-
logger
private static final InternalLogger logger
-
CLEANUP_INTERVAL
private static final int CLEANUP_INTERVAL
- See Also:
- Constant Field Values
-
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:
- Constant Field Values
-
SELECTOR_AUTO_REBUILD_THRESHOLD
private static final int SELECTOR_AUTO_REBUILD_THRESHOLD
-
selectNowSupplier
private final IntSupplier selectNowSupplier
-
selector
private java.nio.channels.Selector selector
The NIOSelector.
-
unwrappedSelector
private java.nio.channels.Selector unwrappedSelector
-
selectedKeys
private SelectedSelectionKeySet selectedKeys
-
provider
private final java.nio.channels.spi.SelectorProvider provider
-
AWAKE
private static final long AWAKE
- See Also:
- Constant Field Values
-
NONE
private static final long NONE
- See Also:
- Constant Field Values
-
nextWakeupNanos
private final java.util.concurrent.atomic.AtomicLong nextWakeupNanos
-
selectStrategy
private final SelectStrategy selectStrategy
-
ioRatio
private volatile int ioRatio
-
cancelledKeys
private int cancelledKeys
-
needsToSelectAgain
private boolean needsToSelectAgain
-
-
Constructor Detail
-
NioEventLoop
NioEventLoop(NioEventLoopGroup parent, java.util.concurrent.Executor executor, java.nio.channels.spi.SelectorProvider selectorProvider, SelectStrategy strategy, RejectedExecutionHandler rejectedExecutionHandler, EventLoopTaskQueueFactory queueFactory)
-
-
Method Detail
-
newTaskQueue
private static java.util.Queue<java.lang.Runnable> newTaskQueue(EventLoopTaskQueueFactory queueFactory)
-
openSelector
private NioEventLoop.SelectorTuple openSelector()
-
selectorProvider
public java.nio.channels.spi.SelectorProvider selectorProvider()
-
newTaskQueue
protected java.util.Queue<java.lang.Runnable> newTaskQueue(int maxPendingTasks)
Description copied from class:SingleThreadEventExecutorCreate a newQueuewhich will holds the tasks to execute. This default implementation will return aLinkedBlockingQueuebut if your sub-class ofSingleThreadEventExecutorwill not do any blocking calls on the thisQueueit may make sense to@Overridethis and return some more performant implementation that does not support blocking operations at all.- Overrides:
newTaskQueuein classSingleThreadEventExecutor
-
newTaskQueue0
private static java.util.Queue<java.lang.Runnable> newTaskQueue0(int maxPendingTasks)
-
register
public void register(java.nio.channels.SelectableChannel ch, int interestOps, NioTask<?> task)Registers an arbitrarySelectableChannel, not necessarily created by Netty, to theSelectorof this event loop. Once the specifiedSelectableChannelis registered, the specifiedtaskwill be executed by this event loop when theSelectableChannelis ready.
-
register0
private void register0(java.nio.channels.SelectableChannel ch, int interestOps, NioTask<?> task)
-
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()
Replaces the currentSelectorof this event loop with newly createdSelectors to work around the infamous epoll 100% CPU bug.
-
registeredChannels
public int registeredChannels()
Description copied from class:SingleThreadEventLoopReturns the number ofChannels registered with thisEventLoopor-1if operation is not supported. The returned value is not guaranteed to be exact accurate and should be viewed as a best effort.- Overrides:
registeredChannelsin classSingleThreadEventLoop
-
rebuildSelector0
private void rebuildSelector0()
-
run
protected void run()
Description copied from class:SingleThreadEventExecutorRun the tasks in theSingleThreadEventExecutor.taskQueue- Specified by:
runin classSingleThreadEventExecutor
-
unexpectedSelectorWakeup
private boolean unexpectedSelectorWakeup(int selectCnt)
-
handleLoopException
private static void handleLoopException(java.lang.Throwable t)
-
processSelectedKeys
private void processSelectedKeys()
-
cleanup
protected void cleanup()
Description copied from class:SingleThreadEventExecutorDo nothing, sub-classes may override- Overrides:
cleanupin classSingleThreadEventExecutor
-
cancel
void cancel(java.nio.channels.SelectionKey key)
-
processSelectedKeysPlain
private void processSelectedKeysPlain(java.util.Set<java.nio.channels.SelectionKey> selectedKeys)
-
processSelectedKeysOptimized
private void processSelectedKeysOptimized()
-
processSelectedKey
private void processSelectedKey(java.nio.channels.SelectionKey k, AbstractNioChannel ch)
-
processSelectedKey
private static void processSelectedKey(java.nio.channels.SelectionKey k, NioTask<java.nio.channels.SelectableChannel> task)
-
closeAll
private void closeAll()
-
invokeChannelUnregistered
private static void invokeChannelUnregistered(NioTask<java.nio.channels.SelectableChannel> task, java.nio.channels.SelectionKey k, java.lang.Throwable cause)
-
wakeup
protected void wakeup(boolean inEventLoop)
- Overrides:
wakeupin classSingleThreadEventExecutor
-
beforeScheduledTaskSubmitted
protected boolean beforeScheduledTaskSubmitted(long deadlineNanos)
Description copied from class:AbstractScheduledEventExecutorCalled from arbitrary non-EventExecutorthreads prior to scheduled task submission. Returnstrueif theEventExecutorthread should be woken immediately to process the scheduled task (if not already awake).If
falseis returned,AbstractScheduledEventExecutor.afterScheduledTaskSubmitted(long)will be called with the same value after the scheduled task is enqueued, providing another opportunity to wake theEventExecutorthread if required.- Overrides:
beforeScheduledTaskSubmittedin classAbstractScheduledEventExecutor- Parameters:
deadlineNanos- deadline of the to-be-scheduled task relative toAbstractScheduledEventExecutor.nanoTime()- Returns:
trueif theEventExecutorthread should be woken,falseotherwise
-
afterScheduledTaskSubmitted
protected boolean afterScheduledTaskSubmitted(long deadlineNanos)
Description copied from class:AbstractScheduledEventExecutorSeeAbstractScheduledEventExecutor.beforeScheduledTaskSubmitted(long). Called only after that method returns false.- Overrides:
afterScheduledTaskSubmittedin classAbstractScheduledEventExecutor- Parameters:
deadlineNanos- relative toAbstractScheduledEventExecutor.nanoTime()- Returns:
trueif theEventExecutorthread should be woken,falseotherwise
-
unwrappedSelector
java.nio.channels.Selector unwrappedSelector()
-
selectNow
int selectNow() throws java.io.IOException- Throws:
java.io.IOException
-
select
private int select(long deadlineNanos) throws java.io.IOException- Throws:
java.io.IOException
-
selectAgain
private void selectAgain()
-
-