补充一个用到Latch的AIO例程

 1 public class AsynChronousSocketChannelRe implements Runnable,CompletionHandler<Void,
 2 AsynChronousSocketChannelRe> {
 3     private AsynchronousSocketChannel socketChannel;
 4     private CountDownLatch latch;
 5     private String host=null;
 6     private int port ;
 7     public AsynChronousSocketChannelRe(String host,int port){
 8         this.host=host;
 9         this.port=port;
10         try{
11         socketChannel=AsynchronousSocketChannel.open();
12         }catch(IOException ex){
13             
14         }
15     }
16     public void run(){
17         socketChannel.connect(new InetSocketAddress(host,port),this,this);
18         latch=new CountDownLatch(1);
19         try{
20             latch.await();
21         }catch(InterruptedException ex){
22             
23         }try{
24             socketChannel.close();
25         }catch(IOException ex){
26             
27         }
28     }
29     @Override
30     public void completed(Void result,AsynChronousSocketChannelRe attachment){
31         byte[] bytes="QUERY TIME ORDER".getBytes();
32         ByteBuffer readBuffer=ByteBuffer.allocate(bytes.length);
33         readBuffer.put(bytes);
34         readBuffer.flip();
35         socketChannel.write(readBuffer, readBuffer, new CompletionHandler<Integer,ByteBuffer>(){
36             @Override
37             public void completed(Integer result,ByteBuffer readBuffers){
38                 if(readBuffers.hasRemaining()){
39                     socketChannel.write(readBuffers, readBuffers,this);
40                 }
41                 else{
42                     ByteBuffer buffer=ByteBuffer.allocate(1024);
43                     socketChannel.read(buffer, buffer, new CompletionHandler<Integer,ByteBuffer>(){
44                         @Override
45                         public void completed(Integer result,ByteBuffer readbuffer){
46                             readbuffer.flip();
47                             byte[] bytes=new byte[readbuffer.remaining()];
48                             readbuffer.get(bytes);
49                             String body;
50                             try{
51                                 body=new String(bytes,"UTF-8");
52                                 System.out.println("Now is :"+body);
53                                 latch.countDown();
54                             }catch(IOException ex){
55                                 
56                             }
57                         }
58                         @Override
59                         public void failed(Throwable ex,ByteBuffer readbuffer){
60                             try{
61                                 socketChannel.close();
62                                 latch.countDown();
63                             }catch(IOException exc){
64                                 
65                             }
66                         }
67                     });
68                     
69             }
70             }
71             @Override
72             public void failed(Throwable exc,ByteBuffer readBuffers){
73                 try{
74                     socketChannel.close();
75                     latch.countDown();
76                 }catch(IOException ex){
77                     
78                 }
79             }
80         });
81         
82     }
83     @Override
84     public void failed(Throwable ex,AsynChronousSocketChannelRe attachment){
85         try{
86         latch.countDown();
87         socketChannel.close();
88     }catch(IOException exc){
89         
90     }
91 
92 }
93 }

 

posted @ 2017-03-05 16:24  倾听枫声  阅读(202)  评论(0编辑  收藏  举报