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 );}}