Home > Java > javaTutorial > body text

Introduction to the implementation of network IO in Java (picture and text)

黄舟
Release: 2017-03-17 10:03:55
Original
1412 people have browsed it

This article mainly introduces the relevant information about the implementation of network IO in Java (BIO, NIO, AIO). Friends in need can refer to

In NetworkProgramming , the most exposed thing is the use of Socket for network communication development. In Java, there are mainly three implementation methods: BIO, NIO, and AIO.

I always seemed to understand the analysis of these three concepts, but the expression was not very clear. The following is a summary to fully clarify the analysis.

1. BIO method

First of all, I use a more popular language to explain:

BIO is blocking IO, each TCP When connecting to the server, you need to create a thread to establish the connection and process the message. If blocking occurs in the middle (for example, when establishing a connection, reading data, or writing data), the thread will also be blocked. In a concurrent situation, N connections require N threads to process.
The disadvantage of this method is that it is very inefficient under concurrent conditions.

The following is a diagram to illustrate the working situation of BIO

##2. NIO method

NIO was proposed by JDK1.4. Let me first explain the working principle of NIO in a simple way:

NIO is non-blocking IO, which is based on

eventdriverThe idea (Reactor thread model). Compared with BIO, NIO uses one thread to manage all Socket channels, which is based on the Selector mechanism. When an event is queried (connection, connection acceptance, reading, writing), it will be forwarded to different processing threads (handler) .

The working application diagram of the Reactor model is given below:


3. AIO method

AIO is proposed by JDK1.7, which is asynchronous IO. AIO adopts Proactor mode. The first thing we should analyze is the difference between AIO and NIO:

(1) NIO notification occurs before Handler;

(2) AIO notification is a callback that occurs after reading and writing, etc. When there is a notification, it means that the relevant operation has ended.

When AIO performs read and write operations, it only needs to call the corresponding read/write method and pass in the CompletionHandler (handler when the action is completed). The CompletionHandler will be called after the action is completed. NIO's notification occurs before the action, when it is readable and writable. After the Selector discovers these events, it notifies and calls the Handler for processing.

The workflow diagram of the Proactor mode is given below:


The above is the detailed content of Introduction to the implementation of network IO in Java (picture and text). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!