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

How to analyze NIO

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Today, I would like to talk to you about how to analyze NIO. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something from this article.

Five Istroke O models under linux

1) blocking Imax O

2) non-blocking Istroke O

3) I use O reuse

4) signal-driven Istroke O

5) Asynchronous IBO

The first four are synchronous, and only the last is asynchronous IO.

1. Synchronous IO: if a thread requests an IO operation, the thread will be blocked before the IO operation is completed!

2. Asynchronous IO: if a thread requests an IO operation, the IO operation will not cause the requesting thread to be blocked!

Synchronous and asynchronous are for the interaction between the user thread and the kernel. Synchronous IO, the user sends out an IO request. If the data is not ready, the user thread or kernel needs to constantly poll whether the data is ready. When the data

When ready, copy the data from the kernel to the user thread.

The key difference between asynchronous IO and synchronization: whether the data copy phase is completed by the user thread or by the kernel

Buffer: three state variables of the buffer: capacity capacity, read-write limit limit, and read-write location position. Need to be understood correctly!

Channels: NIO features, channels are not abstracted from the level of data processed by IO operations, but are represented as links that have been established to entities that support IO operations. Once this connection is established, you can use this

Perform various IO operations on the connection. The exact operation depends on the characteristics of the channel, the general operation of reading and writing, and so on. In java NIO, different entities have different channel implementations, such as file channels and network channels.

Channels are implemented using buffers instead of byte arrays when reading and writing.

Socket channel:

The traditional IO is blocking, and a connection can be established for processing once there is a request. The way that one request corresponds to one thread is obviously not suitable for the situation with heavy load, because each thread takes up resources.

There is also a cost to create a thread, which refers to the implementation of the thread pool to reuse existing thread problems, and when one thread is blocked due to waiting for a network operation, other threads can continue to execute, overall performance and throughput

It's been improved. However, because multithreading is too complex, it is easy to make a lot of hidden errors (*).

JavaNIO provides non-blocking and multiplexed set of byte connections

1. Blocking socket channel

2. Multiplexing set of byte channels

Multiple socket channels are monitored at the same time through a special selector. When an event of interest occurs on one of the socket channels, those channels become available

Can be selected in the selection operation of the selector. The selector can obtain a list of these selected channels through a selection operation, and then process them separately according to the type of event that occurred.

Core: selector, non-blocking socket channel can be registered with selector. "when registering, you need to provide a list of events of interest to the socket channel, including connection completion, receipt of a new connection request,

There is data to read and write data to and so on. These events are defined in the SelectionKey class and can be selected through the select method of selector after registration. "after selection, you can get an available from the objects of the Selector class

A list of socket channels.

3. IP Multicast Channel

Data packets can be transmitted to multiple hosts belonging to the same packet through the Multicast (multicasting) support of the IP protocol. When programs on different hosts need to receive the same data packets, IP multicast is the most natural way.

Each multicast message is received by all hosts belonging to a particular packet. Each multicast packet has a corresponding identifier, which is a Class D IP address ranging from "224.0.0.1" to "239.255.255.255".

The multicast program can choose to join a multicast packet to receive all messages sent to that packet.

Channels are divided into two categories: file channels and Socket channels.

Channel and flow: the channel is bi-directional, while the flow is only from one direction to the other. Channels can read or write or read and write at the same time.

Pipe: Pipe, in a broad sense, a pipe is a pipe used to transfer data one way between two entities. A pipe is usually used to connect the output of one process to the input of another process. The Pipe class implements a pipeline example

However, it is created within a pipeline process (within the java virtual machine process) rather than between processes.

A pipe consists of a pair of channels: a writable sink channel and a readable source channel. Once some bytes are written to the channel of the receiver, they can be written from the source channel in exactly the same order as when they were written

Read these bytes.

Channel:

DatagramChannel for optional channels for Datagram sockets

Pipe.SinkChannel, which represents the writable ending channel of the Pipe.

Pipe.SourceChannel, which represents the readable ending channel of the Pipe.

ServerSocketChannel, an optional channel for stream-oriented listening sockets.

SocketChannel, an optional channel for stream-oriented connection sockets.

After reading the above, do you have any further understanding of how to analyze NIO? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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