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 realize AIO Asynchronous Network programming in Java

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

It is believed that many inexperienced people have no idea about how to realize AIO asynchronous network programming in Java. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

The An in AIO is Asynchronous,AIO, which is asynchronous IO. It is asynchronous and non-blocking. The OS completes all the client's Icano requests and then notifies the server application to start the thread for processing. Generally speaking, our business processing logic becomes a callback function, waiting for the IO operation to be completed and triggered automatically by the system.

When reading and writing, you only need to call the read/write method of API directly. Both methods are asynchronous, and for read operations, when there is a stream to read, the operating system passes the readable stream into the buffer of the read method and notifies the application; for write operations, when the operating system finishes writing the stream passed by the write method, the operating system actively notifies the application. In other words, it can be understood that the read/write methods are asynchronous, and the callback function will be called actively after completion.

AIO is actually an enhancement to NIO, adding a lot of asynchronous classes such as AsynchronousServerSocketChannel,AsynchronousChannel,AsynchronousChannelGroup,CompletionHandler and so on.

In Linux system, the underlying implementation of AIO and NIO is that epoll,epoll itself is a polling model, AIO only wraps another layer of epoll, while in windows system, AIO is implemented through IOCP (completion port). At present, most of the servers are Linux systems, which is one of the reasons why Netty uses NIO instead of AIO. In practice, due to the difference of operating system, the performance of AIO is sometimes not as efficient as NIO, so AIO is not widely used.

AIO server code example:

Public class AIOServer {

Public static void main (String [] args) throws IOException {

/ / Multithreaded version

/ / ExecutorService executorService = Executors.newCachedThreadPool ()

/ / AsynchronousChannelGroup channelGroup =

/ / AsynchronousChannelGroup.withCachedThreadPool (executorService, 1)

/ / AsynchronousServerSocketChannel serverSocketChannel =

/ / AsynchronousServerSocketChannel.open (channelGroup) .bind (new

/ / InetSocketAddress (8080))

/ / single-threaded version

AsynchronousServerSocketChannel serverSocketChannel =

AsynchronousServerSocketChannel.open () .bind (new InetSocketAddress (8080))

ServerSocketChannel.accept (

Null

New CompletionHandler () {

@ Override

Public void completed (AsynchronousSocketChannel client, Object attachment) {

ServerSocketChannel.accept (null, this)

Try {

System.out.println (client.getRemoteAddress ())

ByteBuffer byteBuffer = ByteBuffer.allocate (1024)

Client.read (

ByteBuffer

ByteBuffer

New CompletionHandler () {

@ Override

Public void completed (Integer result, ByteBuffer attachment) {

Attachment.flip ()

Byte [] content = new byte [attachment.limit ()]

Attachment.get (content)

System.out.println (new String (content))

Try {

System.out.println ("Client:" + client.getRemoteAddress ())

} catch (IOException e) {

E.printStackTrace ()

}

}

@ Override

Public void failed (Throwable exc, ByteBuffer attachment) {

System.out.println ("failed:" + exc.getMessage ())

}

});

} catch (Exception e) {

E.printStackTrace ()

}

}

@ Override

Public void failed (Throwable exc, Object attachment) {}

});

While (true) {

Try {

Thread.sleep (1000)

} catch (InterruptedException e) {

E.printStackTrace ()

}

}

}

}

AIO client code example:

Public class AIOClient {

Public static void main (String [] args) throws Exception {

AsynchronousSocketChannel socketChannel = AsynchronousSocketChannel.open ()

SocketChannel.connect (new InetSocketAddress ("127.0.0.1", 8080))

Thread.sleep (1000)

ByteBuffer buffer = ByteBuffer.wrap ("Hello Server" .getBytes ())

SocketChannel.write (buffer) .get ()

}

} after reading the above, have you mastered the method of how to implement AIO asynchronous network programming in Java? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Internet Technology

Wechat

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

12
Report