In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "what is the logic related to NioEventLoop dealing with IO events". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "what is the logic related to NioEventLoop handling IO events" can help you solve the problem.
NioEventLoop's run () method: protected void run () {for (;;) {try {switch (selectStrategy.calculateStrategy (selectNowSupplier, hasTasks () {case SelectStrategy.CONTINUE: continue; case SelectStrategy.SELECT: / / polling io event (1) select (wakenUp.getAndSet (false)) If (wakenUp.get ()) {selector.wakeup ();} default:} cancelledKeys = 0; needsToSelectAgain = false; / / defaults to 50 final int ioRatio = this.ioRatio If (ioRatio = = 100) {try {processSelectedKeys ();} finally {runAllTasks ();}} else {/ / record start time final long ioStartTime = System.nanoTime () Try {/ / process polled key (2) processSelectedKeys ();} finally {/ / calculation time final long ioTime = System.nanoTime ()-ioStartTime / / execute task (3) runAllTasks (ioTime * (100-ioRatio) / ioRatio);} catch (Throwable t) {handleLoopException (t);} / / Code omission}}
First of all, let's look at the judgment of if (ioRatio = = 100). IoRatio is mainly used to control the ratio of the execution time of the processSelectedKeys () method to the execution time of the task queue. IoRatio defaults to 50, so it will go to the next step else.
First record the start time by final long ioStartTime = System.nanoTime (), and then process the polled key by the processSelectedKeys () method
The processSelectedKeys () method private void processSelectedKeys () {if (selectedKeys! = null) {/ / flip () method directly returns the array processSelectedKeysOptimized (selectedKeys.flip ()) of key;} else {processSelectedKeysPlain (selector.selectedKeys ());}}
We know that after selector is optimized through netty, the property selectedKeys will be initialized, so if this property is not empty, it will go to the processSelectedKeysOptimized (selectedKeys.flip ()) method, which operates on the optimized selector.
If it is a non-optimized selector, it will enter the processSelectedKeysPlain (selector.selectedKeys ()) method
SelectedKeys.flip () is the array bound in selectedKey. As we mentioned in the previous section, selectedKeys is actually stored through an array, so if you listen to the event selectedKeys after select () operation, the array will have a value.
The processSelectedKeysOptimized (selectedKeys.flip ()) method private void processSelectedKeysOptimized (SelectionKey [] selectedKeys) {/ / iterates through the for array for (int I = 0 selectedKeys; I + +) {/ / gets the current selectionKey final SelectionKey k = selectedKeys [I]; if (k = = null) {break;} / / sets the current reference to null selectedKeys [I] = null / / get channel (NioSeverSocketChannel) final Object a = k.attachment (); / / if it is AbstractNioChannel, call the processSelectedKey () method to handle the io event if (an instanceof AbstractNioChannel) {processSelectedKey (k, (AbstractNioChannel) a);} else {@ SuppressWarnings ("unchecked") NioTask task = (NioTask) a; processSelectedKey (k, task) } / / Code omission}}
First, iterate through each key in the array through for. After getting the key, clear the corresponding subscript in the array first, because selector does not empty automatically. This is the same meaning as when we use native selector, when we traverse the set of selector.selectedKeys (), we need to execute remove () after we get the key.
Then get the channel registered on key to determine whether channel is AbstractNioChannel or not, usually AbstractNioChannel, so processSelectedKey (k, (AbstractNioChannel) a) is executed here.
ProcessSelectedKey (k, (AbstractNioChannel) a) method private void processSelectedKey (SelectionKey k, AbstractNioChannel ch) {/ / get unsafe final AbstractNioChannel.NioUnsafe unsafe = ch.unsafe () in channel / / if this key is not legal, there may be something wrong with this channel. If (! k.isValid ()) {/ / Code ellipsis} try {/ / if it is legal, get the io event int readyOps = k.readyOps () of key. / / Link event if ((readyOps & SelectionKey.OP_CONNECT)! = 0) {int ops = k.interestOps (); ops & = ~ SelectionKey.OP_CONNECT; k.interestOps (ops); unsafe.finishConnect () } / / write event if ((readyOps & SelectionKey.OP_WRITE)! = 0) {ch.unsafe () .forceFlush () } / / read events and accept link events / / if the current NioEventLoop is a work thread, here is the op_read event / / if the current NioEventLoop is a boss thread, this is the op_accept event if ((readyOps & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT))! = 0 | readyOps = = 0) {unsafe.read () If (! ch.isOpen ()) {return;}} catch (CancelledKeyException ignored) {unsafe.close (unsafe.voidPromise ());}}
We first get the unsafe bound to channel, and then get the event registered by channel
We are concerned about
If ((readyOps & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT))! = 0 | | readyOps = = 0)
This judgment, this judgment believes that it is very clear on the comments that if the current NioEventLoop is a work thread, here is the op_read event, and if the current NioEventLoop is a boss thread, here is the op_accept event
The read () method is then executed through the unsafe object bound by channel to handle link or read-write events
This is the end of the introduction to "what is the logic related to NioEventLoop handling IO events". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.