原文已同步至http://liumian.win/2016/11/23/io-model-and-java/html
学习I/O模型以前,首先要明白几个概念:java
这几个概念每每是成对出现的,咱们经常可以看到同步阻塞
,异步非阻塞
等描述,正由于如此咱们每每在脑海里面是一个模糊的概念 - “哦,他们是这个样子啊,都差很少嘛”。编程
我刚开始接触IO知识的时候,也存在上述的问题,分不清他们的区别。随着学习的深刻,渐渐来到了痛点区域 - 不弄懂全身感受不舒服,非弄懂不可。安全
同步与异步
描述的是用户线程与内核的交互方式:服务器
阻塞和非阻塞
描述的是用户线程调用内核 I/O 操做的方式:多线程
下面来看一种五种常见IO模型的对比,相信你看了这张图片之后很快就会明白同步、异步、阻塞和非阻塞的区别。框架
首先咱们得明白一次IO操做是须要两个阶段的:准备数据(内核空间) -> 数据从内核空间拷贝到用户空间。为何要这么作呢?由于操做系统在内存中划分了两个区域:一个是内核空间,一个是用户空间。内核空间是留给操做系统进行系统服务的,而用户空间就是咱们的程序运行的内存空间。而操做系统为了系统的安全是不容许咱们的程序直接操做内存空间的,因此咱们必须等待操做系统把磁盘上面的内容读入到内核空间,而后拷贝到用户空间才能操做。从图片的右侧也能够清晰的发现这两个阶段。异步
这以为这篇博客总结得很是好,他说:socket
一个 I/O 操做其实分红了两个步骤:发起 I/O 请求和实际的 I/O 操做。 阻塞 I/O 和非阻塞 I/O 的区别在于第一步,发起 I/O 请求是否会被阻塞,若是阻塞直到完成那么就是传统的阻塞 I/O ,若是不阻塞,那么就是非阻塞 I/O 。 同步 I/O 和异步 I/O 的区别就在于第二个步骤是否阻塞,若是实际的 I/O 读写阻塞请求进程,那么就是同步 I/O 。async
好了,通过上面的解释是否是对IO相关知识理解又深入一些了呢?又或者是模糊了许多呢?都不要紧,下面开始进行详细的IO模型分析。
阻塞IO模型(BIO) 若是IO请求没法当即完成,那么当前线程进入阻塞状态。 不论是第一阶段仍是第二阶段,所有阻塞。
非阻塞IO 模型(Non-blinking IO) 第一阶段(准备数据)不会阻塞,第二阶段(拷贝数据到用户空间)会阻塞。 由于第一阶段不会阻塞,因此咱们只有不断的轮询数据在内核空间是否准备完成,这个过程会形成CPU空转,浪费了宝贵的CPU时间。因此不推荐直接使用这种IO模型进行项目开发。
I/O复用模型 从图中咱们能够看到,两个阶段都阻塞了。那么I/O复用模型和阻塞模型有什么区别呢? 进(线)程将一个或者多个感兴趣的事件(可读、可写等)注册在select方法上面,当事件处于就绪状态时意味着数据在用户空间已经准备好(就绪以前为阻塞状态),那么该方法就会返回执行后面的代码,而后又会阻塞在recvfrom(将数据拷贝到用户空间)这个过程直至完成。 若是您以前用过Java中的Selector,可能很容易理解这块知识。
信号驱动I/O模型 这块我不是很熟,《Netty权威指南》是这样解释的: 首先开启套接口信号驱动I/O功能,并经过系统调用sigaction执行一个信号处理函数(此系统调用当即返回,进程继续工做,他是非阻塞的)。当数据准备就绪时,就为该进程生成一个SIGIO信号,经过信号回调通知应用程序调用recvfrom来读取数据,并通知主循环函数处理数据。
异步I/O模型(AIO) 两个阶段均不阻塞线程。工做原理为:通知内核启动某个IO操做,内核将数据复制到用户空间(咱们指定的空间)后通知咱们。这个过程用户线程不会阻塞。
说了这么多你们是否是想问,你不是说Java中的I/O吗?怎么到目前为止跟Java好像一点关系都没有呢?嘿嘿,别急,下面咱们就聊聊Java中的I/O模型~
Java中的I/O模型 首先刚刚说的大多数I/O模型在Java中都有对应的实现。为何是大多数呢?由于信号驱动I/O模型
没有相应的实现。直接上代码~
/** * 阻塞IO * Created by liumian on 2016/11/23. */ public class BlockServer { public static void main(String[] args) { int port = 8080; try { ServerSocket server = new ServerSocket(port); Socket clientSocket = server.accept(); //client do something } catch (IOException e) { e.printStackTrace(); } } }
这就是一个阻塞IO,阻塞在ServerSocket#accept方法上面,直到有数据到达才会执行后面的代码。
/** * 非阻塞IO * Created by liumian on 2016/11/23. */ public class NonBlockServer { public static void main(String[] args) { int port = 8080; Selector selector = null; try { ServerSocketChannel channel = ServerSocketChannel.open(); channel.socket().bind(new InetSocketAddress(port)); //设置为非阻塞IO channel.configureBlocking(false); //打开一个复用器 selector = Selector.open(); //注册感兴趣的事件 channel.register(selector, SelectionKey.OP_ACCEPT); } catch (IOException e) { e.printStackTrace(); } while (true){ try { selector.select(); } catch (IOException e) { e.printStackTrace(); } Set<SelectionKey> keySet = selector.selectedKeys(); Iterator<SelectionKey> iterator = keySet.iterator(); while (iterator.hasNext()){ SelectionKey key = iterator.next(); if (key.isAcceptable()){ //do something } } } } }
在NIO中出现了通道channel
的概念。相对于以前阻塞IO模型中的流 - 只能单向移动(读或者写),它至关于一根水管能够双向移动(既能够写又能够读或者同时进行)。
public class AsyncServer { public static void main(String[] args) { int port = 8080; ExecutorService executor = Executors.newCachedThreadPool(); // create asynchronous server socket channel bound to the default group try (AsynchronousServerSocketChannel asynchronousServerSocketChannel = AsynchronousServerSocketChannel.open()) { if (asynchronousServerSocketChannel.isOpen()) { // set some options asynchronousServerSocketChannel.setOption(StandardSocketOptions.SO_RCVBUF, 4 * 1024); asynchronousServerSocketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); // bind the server socket channel to local address asynchronousServerSocketChannel.bind(new InetSocketAddress(port)); // display a waiting message while ... waiting clients System.out.println("Waiting for connections ..."); while (true) { Future<AsynchronousSocketChannel> asynchronousSocketChannelFuture = asynchronousServerSocketChannel .accept(); try { final AsynchronousSocketChannel asynchronousSocketChannel = asynchronousSocketChannelFuture .get(); Callable<String> worker = new Callable<String>() { @Override public String call() throws Exception { String host = asynchronousSocketChannel.getRemoteAddress().toString(); System.out.println("Incoming connection from: " + host); final ByteBuffer buffer = ByteBuffer.allocateDirect(1024); // transmitting data while (asynchronousSocketChannel.read(buffer).get() != -1) { buffer.flip(); asynchronousSocketChannel.write(buffer).get(); if (buffer.hasRemaining()) { buffer.compact(); } else { buffer.clear(); } } asynchronousSocketChannel.close(); System.out.println(host + " was successfully served!"); return host; } }; executor.submit(worker); } catch (InterruptedException | ExecutionException ex) { System.err.println(ex); System.err.println("\n Server is shutting down ..."); // this will make the executor accept no new threads // and finish all existing threads in the queue executor.shutdown(); // wait until all threads are finished while (!executor.isTerminated()) { } break; } } } else { System.out.println("The asynchronous server-socket channel cannot be opened!"); } } catch (IOException ex) { System.err.println(ex); } } }
/** * 利用线程池来实现伪异步 * Created by liumian on 2016/11/23. */ public class NAsyncServer { public static void main(String[] args) { int port = 8080; ExecutorService executor = Executors.newCachedThreadPool(); try { ServerSocket server = new ServerSocket(port); while (true){ Socket client = server.accept(); executor.execute(new ClientHandler(client)); } } catch (IOException e) { e.printStackTrace(); } } static class ClientHandler implements Runnable{ private Socket socket; public ClientHandler(Socket socket) { this.socket = socket; } @Override public void run() { //do something } } }
总结 经过NIO、AIO咱们能够得到哪些好处?
从JDK的发展能够看到,从阻塞IO到非阻塞IO到异步IO,咱们能够经过灵活的运用IO构建咱们的高性能服务器。不过从JDK发展的过程也能够看出,每每越灵活的操做使用起来越困难,因此《Netty权威指南》做者建议直接使用成熟的NIO框架去构建咱们的服务器而不是使用原生的NIO接口,这样能够避免不少陷阱。
我的感受I/O这些知识不只要多用,还要去想底层是怎么实现的。这样有助于咱们理解为何要这么作~ 之前刚接触异步IO的时候,老是有这些问题:谁帮咱们去完成了IO操做?我如何知道IO操做什么时候完成?IO操做完成之后数据是放在哪里的?等等问题。后面随着学习的深刻,结合操做系统、Java IO API等知识,慢慢也对IO有了本身的理解~~
检查了不少遍,感受写的仍是不够通顺,咬咬牙,硬着头皮发布(逃
参考资料
《Netty 权威指南》