JAVA concurrent processing experience (4) parallel mode and algorithm 7: AIO Network Programming

Source: Internet
Author: User

JAVA concurrent processing experience (4) parallel mode and algorithm 7: AIO Network Programming
I. Preface

We have learned that NIO is a network operation and provides selector blocking operations, but it is still IO synchronization. After I wait for the IO to be ready, I am notified that I am performing the IO operation. So what is AIO: Asynchronized;

Then AIo is an asynchronous operation. --- It is understood that after reading, I will be notified; our business logic is changed to a callback function, and I/O operations are completed, which will be triggered by the system;

Ii. AIO2.1 Server
Package pattern. aio; import pattern. nio. nioServer; 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. byteChannel; import java. nio. channels. completionHandler; import java. nio. channels. spi. abstractInterruptibleChannel; import java. util. concurrent. executionException; import java. util. concurrent. future; import java. util. concurrent. timeUnit; import java. util. concurrent. timeoutException;/*** Created by ycy on 16/1/21. */public class AIOServer {// first, the asynchronous channel public final static int PORT = 65500; private AsynchronousServerSocketChannel server; public AIOServer () throws IOException {server = AsynchronousServerSocketChannel must be applied. open (). bind (new InetSocketAddress (PORT);} public void start () {System. out. println ("Server listen on" + PORT); // The processor server that registers events and events. accept (null, new CompletionHandler () {public void completed (AsynchronousSocketChannel result, Object attachment) {final ByteBuffer buffer = ByteBuffer. allocate (1, 1024); System. out. println (Thread. currentThread (). getName (); Future
 
  
WriteResult = null; try {result. read (buffer ). get (100, TimeUnit. SECONDS); buffer. flip (); writeResult = result. write (buffer);} catch (InterruptedException | ExecutionException e) {e. printStackTrace ();} catch (TimeoutException e) {e. printStackTrace ();} finally {try {server. accept (null, this); writeResult. get (); result. close ();} catch (Exception e) {e. printStackTrace () ;}} public void failed (Throwable exc, Object attachment) {System. out. println ("failed:" + exc) ;}});} public static void main (String [] args) throws IOException, InterruptedException {new AIOServer (). start (); while (true) {Thread. sleep (1000 );}}}
 
2.2 Client
Package pattern. 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;/*** Created by ycy on 16/1/21. */public class AIOClient {public static void main (String [] args) throws IOException, InterruptedException {final AsynchronousSocketChannel channel = AsynchronousSocketChannel. open (); channel. connect (new InetSocketAddress ("127.0.0.1", 65500), null, new CompletionHandler
 
  
() {@ Override public void completed (Void result, Object attachment) {try {final ByteBuffer buffer = ByteBuffer. allocate (1024); channel. read (buffer, buffer, new CompletionHandler
  
   
() {@ Override public void completed (Integer result, ByteBuffer attachment) {buffer. flip (); System. out. println (new String (buffer. array (); try {channel. close ();} catch (IOException e) {e. printStackTrace () ;}@ Override public void failed (Throwable exc, ByteBuffer attachment) {}});} catch (Exception e) {e. printStackTrace () ;}@ Override public void failed (Throwable exc, Object attachment) {}}); // The End Of The main Thread, waiting for the processing of all threads to be completed. sleep (1000 );}}
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.