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 is NIO?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "what is NIO". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

NIO (No-blocking No-blocking O) is non-blocking (you can actually set blocking and non-blocking modes). An ordinary IO stream is blocked when a thread calls read () or write (), the thread is blocked until some data is read, or the data is completely written, and the thread can't do anything during that time. While NIO is in non-blocking mode, if there is no data available, the thread can perform other tasks, and a single thread can manage multiple input and output channels, which greatly improves the utilization of CPU.

IO is stream-oriented, NIO is buffer-oriented, and all data sent to a channel must first be put into the buffer. Similarly, any data read from the channel must be read into the buffer first. In other words, the data will not be read or written to the channel directly, but will be passed through the buffer first.

Selector selector

NIO's selector allows a single thread to monitor multiple input channels, and this thread uses a selector Selector to listen for events on multiple channels Channel by polling, so that a single thread can handle multiple events.

Channels

The channel is the simulation of the flow in the original Istroke O package, and it is a channel through which the application and the operating system interact with each other and deliver the content. The difference between a channel and a Stream is that a stream can only move in one direction, while a channel is bidirectional and can be used for reading, writing, or both. Channel itself cannot access data directly, and Channel can only interact with Buffer.

All channels registered by Selector can only be subclasses that inherit the SelectableChannel class.

Buffer buffer

The buffer is used to store data. Generally, data is written to buffer first, and buffer records how much data is written. Before reading the data, you need to call flip () to change from write mode to read mode. After reading, you need to call clear () to empty the buffer so that it can be written again.

Construction method

ByteBuffer buf = ByteBuffer.allocate (1024)

Write data to buffer

WriteBuffer.put (str)

Read data from Buffer

/ / create a byte array byte [] bytes = new byte [buffer.remaining ()] based on the number of buffer readable bytes; / / copy the buffer readable byte array to the new array buffer.get (bytes); byte aByte = buf.get ()

Get () belongs to relative reading, reading a byte from the position location and position+1 to prepare for the next reading and writing.

Byte aByte = buf.get (int index)

Belongs to absolute read. Read the byte in the bytes at the bottom of the byteBuffer with the subscript index without changing the position.

Buffer.rewind () sets position back to 0, so you can reread all the data in Buffer. Limit remains the same, but still indicates how many elements can be read from Buffer

SelectionKey

When each Channel registers with Selector, a SelectionKey will be created. SelectionKey establishes a relationship between Channel and Selector and maintains channel events.

Call the cancel method to cancel the key, and the canceled key is not immediately removed from the selector, but is added to the cancelledKeys and removed on the next select operation. So when calling a key, you need to use isValid for verification.

When registering events with the Selector object, NIO defines four types: OP_READ (read), OP_WRITE (write), OP_CONNECT (request connection), and OP_ACCEPT (accept connection).

OP_READ: the operating system's read buffer is ready when there is data to read.

OP_WRITE: ready when the operating system write buffer has free space. In general, write buffers have free space, and small chunks of data can be written directly. There is no need to register this operation type, otherwise the condition is constantly ready and a waste of CPU.

OP_CONNECT: ready when the SocketChannel.connect () request connection is successful. This operation is only available to the client.

OP_ACCEPT: ready when a client connection request is received. This operation is for server use only.

This is the end of "what is NIO". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report