This is the fourth day of my participation in the August More text Challenge. For details, see:August is more challenging
preface
Java IO, BIO, NIO
An in-depth analysis of Java IO (I) overview
An in-depth look at The Java IO (II) BIO
Java IO (3) NIO
In this article, we will talk about AIO knowledge.
An overview of the
Since Java 1.7, Java has provided AIO (asynchronous I/O). Java AIO, also known as NIO2.0, provides an asynchronous I/O approach that differs greatly from standard I/O usage.
Java AIO uses a subscription-notification model: An application registers IO listeners with the operating system and then continues to do its thing. When an I/O event occurs in the operating system and data is ready, the operating system proactively notifying applications and triggering corresponding functions.
AIO
The process flow chart of Java AIO is as follows:
- Like synchronous IO, asynchronous IO is supported by the operating system. Microsoft Windows provides an asynchronous IO technology:
IOCP (I/O CompletionPort)
; - Linux because there is no such asynchronous IO technology, so the use of
epoll
(an implementation of multiplexed IO technology described above) simulates asynchronous IO.
Code sample
Client:
package org.example.aio; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousSocketChannel; import java.nio.charset.Charset; import java.util.concurrent.ExecutionException; public class AioClient { public static void main(String[] args) throws IOException, InterruptedException {/ / open a client channel AsynchronousSocketChannel channel = AsynchronousSocketChannel. The open (); Channel. connect(new InetSocketAddress("127.0.0.1", 9988))); // Sleep for one second to wait for the connection to the server thread.sleep (1000); Write (bytebuffer.wrap ("Hello, I'm the client ".getBytes())).get(); } catch (ExecutionException e) { e.printStackTrace(); } try {// read the returned data from the server ByteBuffer ByteBuffer = bytebuffer.allocate (1024); channel.read(byteBuffer).get(); // Write the data in the channel to the Buffer bytebuffer.flip (); String result = Charset.defaultCharset().newDecoder().decode(byteBuffer).toString(); System.out.println(" the client received the return from the server: "+ result); } catch (ExecutionException e) {e.printStackTrace(); }}}Copy the code
Server:
package org.example.aio; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousServerSocketChannel; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.CompletionHandler; import java.nio.charset.Charset; public class AioServer { public AsynchronousServerSocketChannel serverSocketChannel; Public void listen () throws the Exception {/ / open a server channel serverSocketChannel = AsynchronousServerSocketChannel. The open (); serverSocketChannel.bind(new InetSocketAddress(9988)); / / / / listen port 9988 to monitor serverSocketChannel. Accept (this, new CompletionHandler < AsynchronousSocketChannel, AioServer>() { @Override public void completed(AsynchronousSocketChannel client, AioServer Attachment) {try {if (client.isopen ()) {system.out.println (" receive new client connection, address: "+ client.getremoteAddress ())); final ByteBuffer byteBuffer = ByteBuffer.allocate(1024); Client.read (byteBuffer, client, new CompletionHandler<Integer, AsynchronousSocketChannel>() { @Override public void completed(Integer result, AsynchronousSocketChannel attachment) {try {/ / read request and deal with the client sends data byteBuffer. Flip (); String content = Charset.defaultCharset().newDecoder().decode(byteBuffer).toString(); Println (" server received data from client: "+ content); // Send data to the client ByteBuffer writeBuffer = bytebuffer.allocate (1024); writeBuffer.put("Server send".getBytes()); writeBuffer.flip(); attachment.write(writeBuffer).get(); } catch (Exception e) { e.printStackTrace(); } } @Override public void failed(Throwable exc, AsynchronousSocketChannel attachment) { try { exc.printStackTrace(); attachment.close(); } catch (IOException e) { e.printStackTrace(); }}}); } } catch (Exception e) { e.printStackTrace(); } finally {// When a new client comes in, call the accept method directly, and execute recursively. So that multiple clients can block the legislation. The serverSocketChannel. Accept (attachment, this); } } @Override public void failed(Throwable exc, AioServer attachment) { exc.printStackTrace(); }}); } public static void main(String[] args) throws Exception { new AioServer().listen(); Thread.sleep(Integer.MAX_VALUE); }}Copy the code
This combination is complex to use and is only used in very complex distributed situations, such as message synchronization between clusters. For example, Cassandra’s Gossip communication mechanism is asynchronous and non-blocking.
BIO/NIO/AIO
BIO | NIO | AIO | |
---|---|---|---|
IO model | A synchronized block | Synchronous non-blocking (multiplexing) | Asynchronous non-blocking |
Programming difficulty | simple | complex | complex |
reliability | poor | good | good |
throughput | low | high | high |
BIO, NIO, and AIO application scenarios
-
BIO mode is suitable for small and fixed number of connections. This mode requires high server resources, and concurrency is limited to applications. It is the only choice before JDK1.4, but the program is intuitive, simple and easy to understand.
-
NIO mode is suitable for architectures with large number of connections and relatively short (light operation), such as chat server, concurrency is limited to the application, programming is more complex, JDK1.4 began to support.
-
AIO mode is used for architectures with large number of connections and relatively long connections (heavy operation), such as album server, which fully calls the OS to participate in concurrent operation. The programming is complicated, and JDK7 began to support it.
conclusion
Java BIO: Synchronous and blocking, the server implementation pattern is one thread per connection, that is, when the client has a connection request, the server side needs to start a thread to process it. If the connection does not do anything, there is unnecessary thread overhead, of course, can be improved through the thread pool mechanism.
Java NIO: Synchronous non-blocking, server implementation pattern is one request one thread, that is, the client sent connection requests are registered to the multiplexer, the multiplexer polling for connection I/O requests to start a thread for processing.
Java AIO(NIO.2) : Asynchronous, non-blocking, server implementation mode is a valid request for one thread, the CLIENT I/O request is completed by the OS to notify the server application to start the thread to process.
At the end
I am a code is being hit is still trying to advance. If the article is helpful to you, remember to like, follow yo, thank you!