一、JAVA NIO
在介绍NIO编程之前,我们首先需要澄清一个概念:NIO到底是什么的简称?有人称之为New I/O,因为它相对于之前的I/O类库是新增的,所以被称为New I/O,这是它的官方叫法。但是,由于之前老的I/O类库是阻塞I/O,New I/O类库的目标就是要让Java支持非阻塞I/O,所以,更多的人喜欢称之为非阻塞I/O(Non-block I/O),由于非阻塞I/O更能够体现NIO的特点,所以本文使用的NIO都指的是非阻塞I/O。1.1 NIO类库简介
新的输入/输出(NIO)库是在JDK 1.4中引入的。NIO弥补了原来同步阻塞I/O的不足,它在标准Java代码中提供了高速的、面向块的I/O。通过定义包含数据的类,以及通过以块的形式处理这些数据,NIO不用使用本机代码就可以利用低级优化,这是原来的I/O包所无法做到的。下面我们对NIO的一些概念和功能做下简单介绍,以便大家能够快速地了解NIO类库和相关概念。1.1.1 缓冲区Buffer
我们首先介绍缓冲区(Buffer)的概念,Buffer是一个对象,它包含一些要写入或者要读出的数据。在NIO类库中加入Buffer对象,体现了新库与原I/O的一个重要区别。在面向流的I/O中,可以将数据直接写入或者将数据直接读到Stream对象中。 在NIO库中,所有数据都是用缓冲区处理的。在读取数据时,它是直接读到缓冲区中的;在写入数据时,写入到缓冲区中。任何时候访问NIO中的数据,都是通过缓冲区进行操作。 缓冲区实质上是一个数组。通常它是一个字节数组(ByteBuffer),也可以使用其他种类的数组。但是一个缓冲区不仅仅是一个数组,缓冲区提供了对数据的结构化访问以及维护读写位置(limit)等信息。 最常用的缓冲区是ByteBuffer,一个ByteBuffer提供了一组功能用于操作byte数组。除了ByteBuffer,还有其他的一些缓冲区,事实上,每一种Java基本类型(除了Boolean类型)都对应有一种缓冲区,具体如下:- ByteBuffer:字节缓冲区
- CharBuffer:字符缓冲区
- ShortBuffer:短整型缓冲区
- IntBuffer:整形缓冲区
- LongBuffer:长整形缓冲区
- FloatBuffer:浮点型缓冲区
- DoubleBuffer:双精度浮点型缓冲区
缓冲区的类图继承关系图:
每一个Buffer类都是Buffer接口的一个子实例。除了ByteBuffer,每一个 Buffer类都有完全一样的操作,只是它们所处理的数据类型不一样。因为大多数标准I/O操作都使用ByteBuffer,所以它除了具有一般缓冲区的操作之外还提供一些特有的操作,方便网络读写。
1.1.2.通道Channel
Channel是一个通道,可以通过它读取和写入数据,它就像自来水管一样,网络数据通过Channel读取和写入。通道与流的不同之处在于通道是双向的,流只是在一个方向上移动(一个流必须是InputStream或者OutputStream的子类),而且通道可以用于读、写或者同时用于读写。 因为Channel是全双工的,所以它可以比流更好地映射底层操作系统的API。特别是在UNIX网络编程模型中,底层操作系统的通道都是全双工的,同时支持读写操作。Channel的类图继承关系如图:
自顶向下看,前三层主要是Channel接口,用于定义它的功能,后面是一些具体的功能类(抽象类),从类图可以看出,实际上Channel可以分为两大类:分别是用于网络读写的SelectableChannel和用于文件操作的FileChannel。
本文涉及的ServerSocketChannel和SocketChannel都是SelectableChannel的子类,关于它们的具体用法将在后续的代码中体现。1.1.3.多路复用器Selector
Selector是Java NIO编程的基础,熟练地掌握Selector对于掌握NIO编程至关重要。多路复用器提供选择已经就绪的任务的能力。简单来讲,Selector会不断地轮询注册在其上的Channel,如果某个Channel上面有新的TCP连接接入、读和写事件,这个Channel就处于就绪状态,会被Selector轮询出来,然后通过SelectionKey可以获取就绪Channel的集合,进行后续的I/O操作。 一个多路复用器Selector可以同时轮询多个Channel,由于JDK使用了epoll()代替传统的select实现,所以它并没有最大连接句柄1024/2048的限制。这也就意味着只需要一个线程负责Selector的轮询,就可以接入成千上万的客户端,这确实是个非常巨大的进步。NIO服务端序列图:
NIO客户端时序图:
1.1.4 NIO代码示例
服务端代码 MultiplexerTimeServerpackage NIO; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.util.Date; import java.util.Iterator; import java.util.Set; /** * Created by user-hfc on 2017/10/29. */ public class MultiplexerTimeServer implements Runnable { private Selector selector; private ServerSocketChannel servChannel; private volatile boolean stop; /** * 初始化多路复用器,绑定监听端口 */ public MultiplexerTimeServer(int port) { try { selector = Selector.open(); //创建Selector servChannel = ServerSocketChannel.open(); //打开ServerSocketChannel servChannel.configureBlocking(false); servChannel.socket().bind(new InetSocketAddress(port), 1024); //绑定监听地址 servChannel.register(selector, SelectionKey.OP_ACCEPT); //将打开ServerSocketChannel注册到 Selector System.out.println("The time server is start in port : " + port); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } public void stop() { this.stop = true; } @Override public void run() { while (!stop) { try { selector.select(1000); //selector 轮训就绪的key Set<SelectionKey> selectionKeys = selector.selectedKeys(); Iterator<SelectionKey> it = selectionKeys.iterator(); SelectionKey key = null; while (it.hasNext()) { key = it.next(); it.remove(); try { handleInput(key); //处理输入 } catch (Exception e) { if (null != key) { key.cancel(); if (null != key.channel()) { key.channel().close(); } } } } } catch (Exception e2) { e2.printStackTrace(); } } //多路复用器关闭后,所有注册在上面的Channel和pipe等资源都会被自动去注册并关闭 // 所以不需要重复释放资源 if (null != selector) { try { selector.close(); } catch (Exception e3) { e3.printStackTrace(); } } } private void handleInput(SelectionKey key) throws IOException { //处理新接入的请求信息 if (key.isAcceptable()) { //Accept the new connection ServerSocketChannel ssc = (ServerSocketChannel) key.channel(); SocketChannel sc = ssc.accept(); //接受请求 sc.configureBlocking(false); //Add the new connection to the selector sc.register(selector, SelectionKey.OP_READ); //接受请求之后注册监听读取请求 } if (key.isReadable()) { //Read the data SocketChannel sc = (SocketChannel) key.channel(); //KB ByteBuffer readBuffer = ByteBuffer.allocate(1024); /** * 由于我们已经将SocketChannel设置为异步非阻塞模式,因此这里的read()是非阻塞的 * 使用返回值进行判断,有三种可能的结果 * 返回值大于0:读取到字节,对字节进行编解码 * 返回值等于0:没有读取到字节,属于正常场景,忽略 * 返回值等于-1:链路已经关闭,需要关闭SocketChannel,释放资源 */ int readBytes = sc.read(readBuffer); if (readBytes > 0) { //把buffer的当前位置更改成buffer缓冲区的第一个位置 //用于后续对缓冲区的读取操作 readBuffer.flip(); byte[] bytes = new byte[readBuffer.remaining()]; readBuffer.get(bytes); String body = new String(bytes, "utf-8"); System.out.println("The time server receive order : " + body); String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body) ? new Date(System.currentTimeMillis()).toString() : "BAD ORDER"; doWrite(sc, currentTime); } else if (readBytes < 0) { //对端链路关闭 key.cancel(); sc.close(); } else { //读到0字节,忽略 } } } private void doWrite(SocketChannel channel, String response) throws IOException { if (null != response && response.trim().length() > 0) { byte[] bytes = response.getBytes(); ByteBuffer writeBuffer = ByteBuffer.allocate(bytes.length); writeBuffer.put(bytes); writeBuffer.flip(); channel.write(writeBuffer); } } }TimeServer
package NIOInduction.NIO.Server; import java.io.IOException; /** * Created by IntelliJ IDEA 14. * User: karl.zhao * Time: 2015/11/25 0025. */ public class TimeServer { public static void main(String[] args) throws IOException { int port = 8080; if (args != null && args.length > 0) { try { port = Integer.valueOf(args[0]); } catch (NumberFormatException ex) { } } MultiplexerTimeServer timeServer = new MultiplexerTimeServer(port); new Thread(timeServer, "NIO-MultiplexerTimeServer-001").start(); } }客户端代码 TimeClientHandle
package NIOInduction.NIO.Client; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.SocketChannel; import java.util.Iterator; import java.util.Set; /** * Created by IntelliJ IDEA 14. * User: karl.zhao * Time: 2015/11/25 0025. */ public class TimeClientHandle implements Runnable { private String host; private int port; private Selector selector; private SocketChannel socketChannel; private volatile boolean stop; public TimeClientHandle(String host, int port) { this.host = host == null ? "127.0.0.1" : host; this.port = port; try { selector = Selector.open(); socketChannel = SocketChannel.open(); socketChannel.configureBlocking(false); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } public void run() { try { doConnect(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } while (!stop) { try { selector.select(1000); Set<SelectionKey> selectionKeys = selector.selectedKeys(); Iterator<SelectionKey> it = selectionKeys.iterator(); SelectionKey key = null; while (it.hasNext()) { key = it.next(); it.remove(); try { handleInput(key); } catch (Exception e) { if (key != null) { key.cancel(); if (key.channel() != null) key.channel().close(); } } } } catch (Exception e) { e.printStackTrace(); System.exit(1); } } // 多路复用器关闭后,所有注册在上面的Channel和Pipe等资源都会被自动注册并关闭,所以不需要重新释放资源 if (selector != null) { try { selector.close(); } catch (IOException e) { e.printStackTrace(); } } } private void handleInput(SelectionKey key) throws IOException { if (key.isValid()) { // 判断是否连接成功 SocketChannel sc = (SocketChannel) key.channel(); if (key.isConnectable()) { if (sc.finishConnect()) { // Add the new connection to the selector sc.register(selector, SelectionKey.OP_READ); doWrite(sc); } else System.exit(1); // 连接失败,退出进程 } if (key.isReadable()) { // read data ByteBuffer readBuffer = ByteBuffer.allocate(1024); int readBytes = sc.read(readBuffer); if (readBytes > 0) { readBuffer.flip(); byte[] bytes = new byte[readBuffer.remaining()]; readBuffer.get(bytes); String body = new String(bytes, "UTF-8"); System.out.println("Now is : " + body); this.stop = true; } else if (readBytes < 0) { // 对端链路关闭 key.cancel(); sc.close(); } else ; // 读到0字节,忽略 } } } private void doConnect() throws IOException { // 如果连接成功,则注册到多路复用器上,发送请求,读应答 if (socketChannel.connect(new InetSocketAddress(host, port))) { socketChannel.register(selector, SelectionKey.OP_READ); doWrite(socketChannel); } else { socketChannel.register(selector, SelectionKey.OP_CONNECT); } } private void doWrite(SocketChannel sc) throws IOException { byte[] req = "QUERY TIME ORDER".getBytes(); ByteBuffer writeBuffer = ByteBuffer.allocate(req.length); writeBuffer.put(req); writeBuffer.flip(); sc.write(writeBuffer); if (!writeBuffer.hasRemaining()) System.out.println("Send order 2 service succeed"); } }
TimeClient
package NIOInduction.NIO.Client; /** * Created by IntelliJ IDEA 14. * User: karl.zhao * Time: 2015/11/25 0025. */ public class TimeClient { public static void main(String[] agrs) { int port = 8080; String hostAddr = "127.0.0.1"; if (agrs != null && agrs.length == 1) { try { port = Integer.valueOf(agrs[0]); } catch (NumberFormatException ex) { } } else if (agrs != null && agrs.length == 2) { try { hostAddr = agrs[0]; port = Integer.valueOf(agrs[1]); } catch (NumberFormatException ex) { } } for (int i = 1; i <= 1000; i++) { TimeClientHandle tc = new TimeClientHandle(hostAddr, port); Thread tt = new Thread(tc, "线程" + i); try { // tt.sleep(1000); tt.start(); } catch (Exception e) { } } } }
二、JAVA AIO
NIO2.0引入了新的异步通道的概念,并提供了异步文件通道和异步套接字通道的实现。异步通道提供两种方式获取获取操作结果。- 通过java.util.concurrent.Future类来表示异步操作的结果;
- 在执行异步操作的时候传入一个java.nio.channels。
package NIOInduction.AIO.Server; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.CompletionHandler; import java.util.Date; /** * Created by IntelliJ IDEA 14. * User: karl.zhao * Time: 2015/11/26 0026. */ public class ReadCompletionHandler implements CompletionHandler<Integer, ByteBuffer> { private AsynchronousSocketChannel channel; public ReadCompletionHandler(AsynchronousSocketChannel channel) { this.channel = channel; } @Override public void completed(Integer result, ByteBuffer attachment) { attachment.flip(); byte[] body = new byte[attachment.remaining()]; attachment.get(body); try { String req = new String(body, "UTF-8"); System.out.println("the time server seceive order : " + req); String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(req) ? new Date( System.currentTimeMillis() ).toString() : "BAD ORDER"; doWrite(currentTime); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } private void doWrite(String currentTime) { if (currentTime != null && currentTime.trim().length() > 0) { byte[] bytes = (currentTime).getBytes(); ByteBuffer writeBuffer = ByteBuffer.allocate(bytes.length); writeBuffer.put(bytes); writeBuffer.flip(); channel.write(writeBuffer, writeBuffer, new CompletionHandler<Integer, ByteBuffer>() { public void completed(Integer result, ByteBuffer attachment) { // 如果没有发送完成,则继续发送 if (attachment.hasRemaining()) channel.write(attachment, attachment, this); } public void failed(Throwable exc, ByteBuffer attachment) { try { channel.close(); } catch (IOException e) { } } }); } } @Override public void failed(Throwable exc, ByteBuffer attachment) { try { this.channel.close(); } catch (IOException e) { e.printStackTrace(); } } }
AcceptCompletionHandler
package NIOInduction.AIO.Server; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.CompletionHandler; /** * Created by IntelliJ IDEA 14. * User: karl.zhao * Time: 2015/11/26 0026. */ public class AcceptCompletionHandler implements CompletionHandler<AsynchronousSocketChannel, AsyneTimeServerHandler> { @Override public void completed(AsynchronousSocketChannel result, AsyneTimeServerHandler attachment) { attachment.asynchronousServerSocketChannel.accept(attachment, this); ByteBuffer buffer = ByteBuffer.allocate(1024); result.read(buffer, buffer, new ReadCompletionHandler(result)); } @Override public void failed(Throwable exc, AsyneTimeServerHandler attachment) { exc.printStackTrace(); attachment.latch.countDown(); } }
AsyneTimeServerHandler
package NIOInduction.AIO.Server; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.channels.AsynchronousServerSocketChannel; import java.util.concurrent.CountDownLatch; /** * Created by IntelliJ IDEA 14. * User: karl.zhao * Time: 2015/11/26 0026. */ public class AsyneTimeServerHandler implements Runnable { private int port; CountDownLatch latch;/* 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。*/ AsynchronousServerSocketChannel asynchronousServerSocketChannel; public AsyneTimeServerHandler(int port) { this.port = port; try { asynchronousServerSocketChannel = AsynchronousServerSocketChannel.open(); asynchronousServerSocketChannel.bind(new InetSocketAddress(port)); System.out.println("The Server start in port : " + port); } catch (IOException e) { e.printStackTrace(); } } public void run() { latch = new CountDownLatch(1); doAccept(); try { latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } } public void doAccept() { asynchronousServerSocketChannel.accept(this, new AcceptCompletionHandler()); } }
TimeServer
package NIOInduction.AIO.Server; import java.io.IOException; /** * Created by IntelliJ IDEA 14. * User: karl.zhao * Time: 2015/11/26 0026. */ public class TimeServer { public static void main(String[] args) throws IOException { int port = 8080; if (args != null && args.length > 0) { try { port = Integer.valueOf(args[0]); } catch (NumberFormatException ex) { } } AsyneTimeServerHandler timeServer = new AsyneTimeServerHandler(port); new Thread(timeServer, "xxxx").start(); } }
客户端代码 AsyncTimeClientHandler
package NIOInduction.AIO.Client; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.CompletionHandler; import java.util.concurrent.CountDownLatch; /** * Created by IntelliJ IDEA 14. * User: karl.zhao * Time: 2015/11/26 0026. */ public class AsyncTimeClientHandler implements CompletionHandler<Void, AsyncTimeClientHandler>, Runnable { private AsynchronousSocketChannel client; private String host; private int port; private CountDownLatch latch; public AsyncTimeClientHandler(String host, int port) { this.host = host; this.port = port; try { client = AsynchronousSocketChannel.open(); } catch (IOException e) { e.printStackTrace(); } } @Override public void run() { latch = new CountDownLatch(1); client.connect(new InetSocketAddress(host, port), this, this); try { latch.await(); } catch (InterruptedException e1) { e1.printStackTrace(); } try { client.close(); } catch (IOException e) { e.printStackTrace(); } } @Override public void completed(Void result, AsyncTimeClientHandler attachment) { byte[] req = "QUERY TIME ORDER".getBytes(); ByteBuffer writeBuffer = ByteBuffer.allocate(req.length); writeBuffer.put(req); writeBuffer.flip(); client.write(writeBuffer, writeBuffer, new CompletionHandler<Integer, ByteBuffer>() { @Override public void completed(Integer result, ByteBuffer buffer) { if (buffer.hasRemaining()) { client.write(buffer, buffer, this); } else { ByteBuffer readBuffer = ByteBuffer.allocate(1024); client.read( readBuffer, readBuffer, new CompletionHandler<Integer, ByteBuffer>() { @Override public void completed(Integer result, ByteBuffer buffer) { buffer.flip(); byte[] bytes = new byte[buffer .remaining()]; buffer.get(bytes); String body; try { body = new String(bytes, "UTF-8"); System.out.println("Now is : " + body); latch.countDown(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } @Override public void failed(Throwable exc, ByteBuffer attachment) { try { client.close(); latch.countDown(); } catch (IOException e) { // ingnore on close } } }); } } @Override public void failed(Throwable exc, ByteBuffer attachment) { try { client.close(); latch.countDown(); } catch (IOException e) { // ingnore on close } } }); } @Override public void failed(Throwable exc, AsyncTimeClientHandler attachment) { exc.printStackTrace(); try { client.close(); latch.countDown(); } catch (IOException e) { e.printStackTrace(); } } }
TimeClient
package NIOInduction.AIO.Client; /** * Created by IntelliJ IDEA 14. * User: karl.zhao * Time: 2015/11/26 0026. */ public class TimeClient { public static void main(String[] args) { int port = 8080; if (args != null && args.length > 0) { try { port = Integer.valueOf(args[0]); } catch (NumberFormatException e) { // 采用默认值 } } for (int i = 1; i <= 20; i++) { AsyncTimeClientHandler tc = new AsyncTimeClientHandler("127.0.0.1", port); Thread tt = new Thread(tc, "线程" + i); try { // tt.sleep(1000); tt.start(); } catch (Exception e) { } } } }
三、4种I/O对比
3.1 概念澄清
1.异步非阻塞IO
很多人喜欢将JDK1.4提供的NIO框架称为异步非阻塞I/O,但是,如果严格按照UNIX网络编程模型和JDK的实现进行区分,实际上它只能被称为非阻塞I/O,不能叫异步非阻塞I/O。在早期的JDK1.4和1.5 update10版本之前,JDK的Selector基于select/poll模型实现,它是基于I/O复用技术的非阻塞I/O,不是异步I/O。在JDK1.5 update10和Linux core2.6以上版本,Sun优化了Selctor的实现,它在底层使用epoll替换了select/poll,上层的API并没有变化,可以认为是JDK NIO的一次性能优化,但是它仍旧没有改变I/O的模型。 由JDK1.7提供的NIO2.0,新增了异步的套接字通道,它是真正的异步I/O,在异步I/O操作的时候可以传递信号变量,当操作完成之后会回调相关的方法,异步I/O也被称为AIO。2.多路复用器Selector
在前面的章节我们介绍过Java NIO的实现关键是多路复用I/O技术,多路复用的核心就是通过Selector来轮询注册在其上的Channel,当发现某个或者多个Channel处于就绪状态后,从阻塞状态返回就绪的Channel的选择键集合,进行I/O操作。由于多路复用器是NIO实现非阻塞I/O的关键,它又是主要通过Selector实现的,所以本文将Selector翻译为多路复用器,与其他技术书籍所说的选择器是同一个东西。3.伪异步IO
伪异步I/O的概念完全来源于实践。在JDK NIO编程没有流行之前,为了解决Tomcat通信线程同步I/O导致业务线程被挂住的问题,大家想到了一个办法:在通信线程和业务线程之间做个缓冲区,这个缓冲区用于隔离I/O线程和业务线程间的直接访问,这样业务线程就不会被I/O线程阻塞。而对于后端的业务侧来说,将消息或者Task放到线程池后就返回了,它不再直接访问I/O线程或者进行I/O读写,这样也就不会被同步阻塞。类似的设计还包括前端启动一组线程,将接收的客户端封装成Task,放到后端的线程池执行,用于解决一连接一线程问题。像这样通过线程池做缓冲区的做法,本书中习惯于称它为伪异步I/O,而官方并没有伪异步I/O这种说法,请大家注意。3.2 不同IO模型对比
标签:java,NIO,port,IO,import,JAVA,ByteBuffer,public From: https://www.cnblogs.com/MuXinu/p/18091388