|
| 1 | +# AIO |
| 2 | + |
| 3 | +IO: BIO ,Blocking IO, 同步 阻塞式IO |
| 4 | + |
| 5 | +NIO:new IO, Non Blocking IO , 同步非阻塞式IO |
| 6 | + |
| 7 | +AIO: (就是给NIO增加了几种类型的Channel), NIO2.0, 异步 非阻塞式IO |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +AIO增加的Channel: 1.(文件)AsynchronousFileChannel 2.通信,(服务端)AsynchronousServerSocketChannel,(客户端)AsynchronousSocketChannel |
| 12 | + |
| 13 | +简单理解:AIO就是比NIO多了以上几个Channel,这些channel支持异步操作 |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +示例: |
| 18 | + |
| 19 | +NIO和AIO的区别: |
| 20 | + |
| 21 | +NIO:如果某个Channel中已经准备好了客户端数据,就来通知我(就来通知服务端处理) |
| 22 | + |
| 23 | +AIO:如果某个Channel中已经准备好了客户端数据 并且已经将数据读取完毕,就来通知我(就来通知服务端处理) |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +AIO实现逻辑: future模式、回调函数 |
| 34 | + |
| 35 | +future模式:当服务端接收到请求后,立刻给出一个”假响应|中间结果“ ,同时开辟一个线程 去真正的处理这件事情,当把事情真实完毕之后,再更新刚才的”假响应|中间结果“。 |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +回调函数: 例如ajax,简单的将,就是 当服务端将请求处理完毕后,将结果返回到的函数,就称为 回调函数。 |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +示例:future模式 |
| 44 | + |
| 45 | +```java |
| 46 | +package aio; |
| 47 | + |
| 48 | +import java.nio.ByteBuffer; |
| 49 | +import java.nio.channels.AsynchronousFileChannel; |
| 50 | +import java.nio.file.Paths; |
| 51 | +import java.util.concurrent.Future; |
| 52 | + |
| 53 | +/* |
| 54 | + * Created by 颜群 |
| 55 | + */ |
| 56 | +public class AIODemo01 { |
| 57 | + |
| 58 | + //读文件,future模式 |
| 59 | + public static void test01() throws Exception{ |
| 60 | + AsynchronousFileChannel channel = AsynchronousFileChannel.open(Paths.get("d:/a.txt")); |
| 61 | + ByteBuffer buffer = ByteBuffer.allocate(1024*10); |
| 62 | + |
| 63 | + //future模式 假设文件大小3312 |
| 64 | + Future<Integer> future = channel.read(buffer, 0);//1.响应一个假结果 2.开辟一个线程真正的处理 |
| 65 | + |
| 66 | + //两个线程:一个线程 自上而下顺序执行 ,另一个线程 执行read,即读取文件 |
| 67 | + |
| 68 | + //验证两个线程并发执行:当 read没有结束时,一个线程在执行read,另一个线程在打印.. |
| 69 | + while(!future.isDone()){ |
| 70 | + System.out.println("在read的同时,Main在执行其他事情..."); |
| 71 | + } |
| 72 | + |
| 73 | + //假结果 (get()方法会一直阻塞,直到“真正处理的线程”将结果更新后,才停止阻塞) |
| 74 | + //future.get()当没有读完文件时,返回值(即读取的数据<3312),当文件读取完毕后,更新返回值位3312 |
| 75 | + Integer readNumber = future.get(); |
| 76 | + |
| 77 | + buffer.flip() ; |
| 78 | + System.out.println("readNumber:"+ readNumber); |
| 79 | + System.out.println( new String(buffer.array(),0,buffer.limit()) ); |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + public static void main(String[] args) throws Exception { |
| 84 | + test01() ; |
| 85 | + |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +``` |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +回调示例: |
| 94 | + |
| 95 | +```java |
| 96 | + |
| 97 | + public static void test02() throws Exception{//回调 读 |
| 98 | + AsynchronousFileChannel channel = AsynchronousFileChannel.open(Paths.get("d:/a.txt")); |
| 99 | + ByteBuffer buffer = ByteBuffer.allocate(1024 * 10); |
| 100 | + |
| 101 | + //回调 |
| 102 | + channel.read(buffer, 0, null, new CompletionHandler<Integer, Object>() { |
| 103 | + //开辟的新线程(真实读取数据的线程 ) |
| 104 | + //成功(数据读完了) |
| 105 | + @Override |
| 106 | + public void completed(Integer result, Object attachment) { |
| 107 | + buffer.flip() ; |
| 108 | + System.out.println( new String( buffer.array(),0,buffer.limit() )); |
| 109 | + System.out.println("读取完毕..."); |
| 110 | + } |
| 111 | + |
| 112 | + //失败 |
| 113 | + |
| 114 | + @Override |
| 115 | + public void failed(Throwable exc, Object attachment) { |
| 116 | + |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + |
| 121 | + //main线程 |
| 122 | + while(true){ |
| 123 | + System.out.println("在read完毕以前,Main可以异步做其他事情..."); |
| 124 | + Thread.sleep(100); |
| 125 | + } |
| 126 | + } |
| 127 | +``` |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +future:写 |
| 134 | + |
| 135 | +回调:写 |
| 136 | + |
| 137 | +```java |
| 138 | + |
| 139 | + public static void test03() throws Exception {//future 写 |
| 140 | + Path path = Paths.get("d:/b.txt"); |
| 141 | + if( Files.exists( path) ){ |
| 142 | + Files.delete( path); |
| 143 | + } |
| 144 | + AsynchronousFileChannel channel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW); |
| 145 | + ByteBuffer buffer = ByteBuffer.allocate(1024); |
| 146 | + |
| 147 | + buffer.put( "helloworld" .getBytes()) ;//写到buffer |
| 148 | + buffer.flip() ; |
| 149 | + |
| 150 | + Future<Integer> future = channel.write(buffer, 0);//写到文件中 |
| 151 | + buffer.clear() ; |
| 152 | + |
| 153 | + while(!future.isDone()){ |
| 154 | + System.out.println("main异步执行其他事情..."); |
| 155 | + } |
| 156 | + |
| 157 | + Integer result = future.get(); |
| 158 | + System.out.println("写完毕,写入的字节数:"+result); |
| 159 | + } |
| 160 | + |
| 161 | + public static void test04() throws Exception {//回调: 写 |
| 162 | + Path path = Paths.get("d:/c.txt"); |
| 163 | + if( Files.exists( path) ){ |
| 164 | + Files.delete( path); |
| 165 | + } |
| 166 | + AsynchronousFileChannel channel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW); |
| 167 | + ByteBuffer buffer = ByteBuffer.allocate(1024); |
| 168 | + |
| 169 | + buffer.put( "helloworld" .getBytes()) ;//写到buffer |
| 170 | + buffer.flip() ; |
| 171 | + |
| 172 | + channel.write(buffer, 0, null, new CompletionHandler<Integer, Object>() { |
| 173 | + @Override |
| 174 | + public void completed(Integer result, Object attachment) { |
| 175 | + System.out.println("写入完毕,共写入了字节数:" +result ); |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public void failed(Throwable exc, Object attachment) { |
| 180 | + exc.printStackTrace(); |
| 181 | + } |
| 182 | + }) ; |
| 183 | + |
| 184 | + |
| 185 | + while(true){ |
| 186 | + System.out.println("main...."); |
| 187 | + Thread.sleep(100); |
| 188 | + |
| 189 | + } |
| 190 | + |
| 191 | + } |
| 192 | +k |
| 193 | +``` |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | + |
0 commit comments