知识点整理-bio、nio的简单demo
- BIO
package com.io.bio; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; public class IOServer { /** * Server服务端首先创建ServerSocket监听8000端口,然后创建线程不断调用阻塞方法 serversocket.accept()获取新的连接,当获取到新的连接给每条连接创建新的线程负责从该连接中读取数据,然后读取数据是以字节流的方式 * * @param args * @throws IOException */ public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8000); //接收新连接线程 new Thread(() -> { try { //(1)阻塞方法获取新的连接 Socket socket = serverSocket.accept(); //(2)每一个新的连接都创建一个线程,负责读取数据 new Thread(() -> { try { byte[] data = new byte[1024]; InputStream inputStream = socket.getInputStream(); while (true) { int len; //(3)按照字节流方式读取数据 while ((len = inputStream.read(data)) != -1) { System.out.println(new String(data, 0, len)); } } } catch (IOException e) { e.printStackTrace(); } }).start(); } catch (IOException e) { e.printStackTrace(); } }).start(); } }
package com.io.bio; import java.io.IOException; import java.net.Socket; import java.util.Date; public class IOClient { /** * Client客户端连接服务端8000端口每隔2秒向服务端写带有时间戳的 "hello world" * * @param args */ public static void main(String[] args) { new Thread(() -> { try { Socket socket = new Socket("127.0.0.1", 8000); while (true) { try { socket.getOutputStream().write((new Date() + ": hello world").getBytes()); socket.getOutputStream().flush(); Thread.sleep(2000); } catch (Exception e) { e.printStackTrace(); } } } catch (IOException e) { e.printStackTrace(); } }).start(); } }
传统的IO模型每个连接创建成功都需要一个线程来维护,每个线程包含一个while死循环,那么1w个连接对应1w个线程,继而1w个while死循环带来如下几个问题:
1.线程资源受限:线程是操作系统中非常宝贵的资源,同一时刻有大量的线程处于阻塞状态是非常严重的资源浪费,操作系统耗不起;
2.线程切换效率低下:单机CPU核数固定,线程爆炸之后操作系统频繁进行线程切换,应用性能急剧下降;
3.数据读写是以字节流为单位效率不高:每次都是从操作系统底层一个字节一个字节地读取数据.
- NIO
package com.io.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.nio.charset.Charset; import java.util.Iterator; import java.util.Set; public class NIOServer { /** * serverSelector负责轮询是否有新的连接,clientSelector负责轮询连接是否有数据可读. * 服务端监测到新的连接不再创建一个新的线程,而是直接将新连接绑定到clientSelector上,这样不用IO模型中1w个while循环在死等 * clientSelector被一个while死循环包裹,如果在某一时刻有多条连接有数据可读通过 clientSelector.select(1)方法轮询出来进而批量处理 * 数据的读写以内存块为单位 * * @param args * @throws IOException */ public static void main(String[] args) throws IOException { Selector serverSelector = Selector.open(); Selector clientSelector = Selector.open(); new Thread(() -> { try { ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.socket().bind(new InetSocketAddress(8000)); serverSocketChannel.configureBlocking(false); serverSocketChannel.register(serverSelector, SelectionKey.OP_ACCEPT); while (true) { // 轮询监测是否有新的连接 if (serverSelector.select(1) > 0) { Set<SelectionKey> selectionKeys = serverSelector.selectedKeys(); Iterator<SelectionKey> keyIterator = selectionKeys.iterator(); while (keyIterator.hasNext()) { SelectionKey selectionKey = keyIterator.next(); if (selectionKey.isAcceptable()) { try { //(1)每来一个新连接不需要创建一个线程而是直接注册到clientSelector SocketChannel socketChannel = ((ServerSocketChannel) selectionKey.channel()).accept(); socketChannel.configureBlocking(false); socketChannel.register(clientSelector, SelectionKey.OP_READ); } finally { keyIterator.remove(); } } } } } } catch (IOException e) { e.printStackTrace(); } }).start(); new Thread(() -> { try { while (true) { // (2)批量轮询是否有哪些连接有数据可读 if (clientSelector.select(1) > 0) { Set<SelectionKey> selectionKeys = serverSelector.selectedKeys(); Iterator<SelectionKey> keyIterator = selectionKeys.iterator(); while (keyIterator.hasNext()) { SelectionKey selectionKey = keyIterator.next(); if (selectionKey.isReadable()) { try { SocketChannel socketChannel = (SocketChannel) selectionKey.channel(); ByteBuffer byteBuffer = ByteBuffer.allocate(1024); //(3)读取数据以块为单位批量读取 socketChannel.read(byteBuffer); byteBuffer.flip(); System.out.println(Charset.defaultCharset().newDecoder().decode(byteBuffer) .toString()); } finally { keyIterator.remove(); selectionKey.interestOps(SelectionKey.OP_READ); } } } } } } catch (IOException e) { e.printStackTrace(); } }).start(); } }
1.线程资源受限:NIO编程模型新来一个连接不再创建一个新的线程,把这条连接直接绑定到某个固定的线程,然后这条连接所有的读写都由该线程来负责.把这么多while死循环变成一个死循环,这个死循环由一个线程控制,一条连接来了,不创建一个while死循环去监听是否有数据可读,直接把这条连接注册到Selector上,然后通过检查Selector批量监测出有数据可读的连接进而读取数据.
2.线程切换效率低下:线程数量大大降低,线程切换效率因此也大幅度提高.
3.数据读写是以字节流为单位效率不高:NIO维护一个缓冲区每次从这个缓冲区里面读取一块的数据,数据读写不再以字节为单位,而是以字节块为单位.