Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the components of NIO

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what are the components of NIO". In daily operation, I believe many people have doubts about what components of NIO. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what are the components of NIO?" Next, please follow the editor to study!

Buffer: interacts with Channel, where data is read into the buffer from Channel and written to Channel from the buffer

Flip method: reverse this buffer, give position to limit, and then set position to 0, which is actually switching read-write mode

Clear method: clear this buffer, set position to 0, and give the value of capacity to limit.

Rewind method: rewind this buffer and set position to 0

DirectByteBuffer reduces a copy of system space to user space. However, Buffer creation and destruction are more expensive and uncontrollable, and memory pools are often used to improve performance. Direct buffers are mainly allocated to large, persistent buffers that are vulnerable to the native I _ swap O operation of the underlying system. If the amount of data is relatively small in the case of small and medium-sized applications, you can consider using heapBuffer, managed by JVM.

Channel: indicates that the open connection between the IO source and the destination is bidirectional, but cannot access the data directly and can only interact with the Buffer. As can be seen from the source code, both the read method and the write method of FileChannel cause the data to be copied twice!

Selector enables a single thread to manage multiple Channel,open methods to create Selector,register methods to register channels with the multiplexer, the types of events that can be listened for: read, write, connection, accept. A SelectionKey is generated after registering the event: it represents the registration relationship between SelectableChannel and Selector, and the wakeup method: causes the first selection operation that has not yet been returned to return immediately, waking up

The reasons are: new channel or events are registered; channel is closed and unregistered; higher priority events are triggered (such as timer events) and want to be handled in a timely manner.

The implementation class of Selector in Linux is EPollSelectorImpl, which is delegated to EPollArrayWrapper implementation, in which three native methods encapsulate epoll, while EPollSelectorImpl. The implRegister method registers the event with the epoll instance by calling epoll_ctl, and adds the correspondence between the registered file descriptor (fd) and the SelectionKey to the fdToKey, which maintains the mapping between the file descriptor and the SelectionKey.

FdToKey can sometimes become very large because there are so many Channel registered with Selector (millions of connections); expired or expired Channel is not shut down in time. FdToKey is always read serially, while reading is done in the select method, which is not thread-safe.

Pipe: an one-way data connection between two threads in which the data is written to the sink channel and read from the source channel

NIO server-side establishment process: Selector.open (): open a Selector;ServerSocketChannel.open (): create a server-side Channel;bind (): bind to a port. And configure non-blocking mode; register (): register Channel and interested events to Selector; select () poll to get ready events.

At this point, the study of "what are the components of NIO" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report