做业务 哪个网站比较好,新河企业做网站,discuz做门户网站,做网站还是博客华为二面#xff01;#xff01;#xff01;面试官直接问我Java中到底什么是NIO#xff1f;这不是直接送分题#xff1f;#xff1f;#xff1f;什么是NIO缓冲区(Buffer)缓冲区类型获取缓冲区核心属性核心方法非直接缓冲区和直接缓冲区非直接缓冲区直接缓冲区通道(Chann…
华为二面面试官直接问我Java中到底什么是NIO这不是直接送分题什么是NIO缓冲区(Buffer)缓冲区类型获取缓冲区核心属性核心方法非直接缓冲区和直接缓冲区非直接缓冲区直接缓冲区通道(Channel)Java Channel获得通道的方法对象调用getChannel() 方法getChannel()非直接缓冲区open()直接缓冲区通道间直接传输直接缓冲区VS非直接缓冲区分散和聚集非阻塞式网络通信概念阻塞式网络通信非阻塞式网络通信选择器什么是NIO 
Java NIONew IO是从Java 1.4版本开始引入的一个新的IO API可以替代标准的Java IO API。NIO与原来的IO有同样的作用和目的但是使用的方式完全不同NIO支持面向缓冲区的、基于通道的IO操作。NIO将以更加高效的方式进行文件的读写操作。 
IONIO面向流(Stream Oriented)面向缓冲区(Buffer Oriented)阻塞IO(Blocking IO)非阻塞IO(NonBlocking IO)选择器(Selectors)
底层原理可见操作系统-文件IO 
缓冲区(Buffer) 
缓冲区类型 
Buffer 就像一个数组可以保存多个相同类型的数据。根据数据类型不同(boolean 除外) 有以下Buffer 常用子类 
ByteBufferCharBufferShortBufferIntBufferLongBufferFloatBufferDoubleBuffer 
各种类型的缓冲区中都有一个对应类型的数组如 
ByteBuffer 
final byte[] hb;                  // Non-null only for heap buffersCopy 
IntBuffer 
final int[] hb;                  // Non-null only for heap buffers 获取缓冲区 
通过allocate方法可以获取一个对应缓冲区的对象它是缓冲区类的一个静态方法 
例 
// 获取一个容量大小为1024字节的字节缓冲区
ByteBuffer byteBuffer  ByteBuffer.allocate(1024); 
核心属性 
缓冲区的父类Buffer中有几个核心属性如下 
// Invariants: mark  position  limit  capacity
private int mark  -1;
private int position  0;
private int limit;
private int capacity;Copy 
capacity缓冲区的容量。通过构造函数赋予一旦设置无法更改limit缓冲区的界限。位于limit 后的数据不可读写。缓冲区的限制不能为负并且不能大于其容量position下一个读写位置的索引类似PC。缓冲区的位置不能为负并且不能大于limitmark记录当前position的值。position被改变后可以通过调用reset() 方法恢复到mark的位置。 
以上四个属性必须满足以下要求 
mark  position  limit  capacity 
核心方法 
put()方法 
put()方法可以将一个数据放入到缓冲区中。进行该操作后postition的值会1指向下一个可以放入的位置。capacity  limit 为缓冲区容量的值。 flip()方法 
flip()方法会切换对缓冲区的操作模式由写-读 / 读-写进行该操作后 如果是写模式-读模式position  0  limit 指向最后一个元素的下一个位置capacity不变如果是读-写则恢复为put()方法中的值  get()方法 
get()方法会读取缓冲区中的一个值进行该操作后position会1如果超过了limit则会抛出异常 
rewind()方法 
该方法只能在读模式下使用rewind()方法后会恢复position、limit和capacity的值变为进行get()前的值 
clean()方法 
clean()方法会将缓冲区中的各个属性恢复为最初的状态position  0, capacity  limit此时缓冲区的数据依然存在处于“被遗忘”状态下次进行写操作时会覆盖这些数据 mark()和reset()方法 
mark()方法会将postion的值保存到mark属性中reset()方法会将position的值改为mark中保存的值 
使用展示 
import java.nio.ByteBuffer;public class demo1 {public static void main(String[] args) {ByteBuffer byteBuffer  ByteBuffer.allocate(1024);System.out.println(放入前参数);System.out.println(position   byteBuffer.position());System.out.println(limit   byteBuffer.limit());System.out.println(capacity   byteBuffer.capacity());System.out.println();System.out.println(------put()------);System.out.println(放入3个数据);byte bt  1;byteBuffer.put(bt);byteBuffer.put(bt);byteBuffer.put(bt);System.out.println(放入后参数);System.out.println(position   byteBuffer.position());System.out.println(limit   byteBuffer.limit());System.out.println(capacity   byteBuffer.capacity());System.out.println();System.out.println(------flip()-get()------);System.out.println(读取一个数据);// 切换模式byteBuffer.flip();byteBuffer.get();System.out.println(读取后参数);System.out.println(position   byteBuffer.position());System.out.println(limit   byteBuffer.limit());System.out.println(capacity   byteBuffer.capacity());System.out.println();System.out.println(------rewind()------);byteBuffer.rewind();System.out.println(恢复后参数);System.out.println(position   byteBuffer.position());System.out.println(limit   byteBuffer.limit());System.out.println(capacity   byteBuffer.capacity());System.out.println();System.out.println(------clear()------);// 清空缓冲区这里只是恢复了各个属性的值但是缓冲区里的数据依然存在// 但是下次写入的时候会覆盖缓冲区中之前的数据byteBuffer.clear();System.out.println(清空后参数);System.out.println(position   byteBuffer.position());System.out.println(limit   byteBuffer.limit());System.out.println(capacity   byteBuffer.capacity());System.out.println();System.out.println(清空后获得数据);System.out.println(byteBuffer.get());}
} 
放入前参数
position 0
limit 1024
capacity 1024------put()------
放入3个数据
放入后参数
position 3
limit 1024
capacity 1024------flip()-get()------
读取一个数据
读取后参数
position 1
limit 3
capacity 1024------rewind()------
恢复后参数
position 0
limit 3
capacity 1024------clear()------
清空后参数
position 0
limit 1024
capacity 1024清空后获得数据
1Process finished with exit code 0 
非直接缓冲区和直接缓冲区 
非直接缓冲区 
通过allocate()方法获取的缓冲区都是非直接缓冲区。这些缓冲区是建立在JVM堆内存之中的。 
public static ByteBuffer allocate(int capacity) {if (capacity  0)throw new IllegalArgumentException();// 在堆内存中开辟空间return new HeapByteBuffer(capacity, capacity);
}HeapByteBuffer(int cap, int lim) {        // package-private// new byte[cap] 创建数组在堆内存中开辟空间super(-1, 0, lim, cap, new byte[cap], 0);/*hb  new byte[cap];offset  0;*/
} 
通过非直接缓冲区想要将数据写入到物理磁盘中或者是从物理磁盘读取数据。都需要经过JVM和操作系统数据在两个地址空间中传输时会copy一份保存在对方的空间中。所以费直接缓冲区的读取效率较低.。 直接缓冲区 
只有ByteBuffer可以获得直接缓冲区通过allocateDirect()获取的缓冲区为直接缓冲区这些缓冲区是建立在物理内存之中的。 
public static ByteBuffer allocateDirect(int capacity) {return new DirectByteBuffer(capacity);
}DirectByteBuffer(int cap) {                   // package-private...// 申请物理内存boolean pa  VM.isDirectMemoryPageAligned();...
} 
直接缓冲区通过在操作系统和JVM之间创建物理内存映射文件加快缓冲区数据读/写入物理磁盘的速度。放到物理内存映射文件中的数据就不归应用程序控制了操作系统会自动将物理内存映射文件中的数据写入到物理内存中。 通道(Channel) 
Channel由java.nio.channels 包定义的。Channel 表示IO 源与目标打开的连接。Channel 类似于传统的“流”。只不过Channel 本身不能直接访问数据Channel 只能与Buffer 进行交互 。 
应用程序进行读写操作调用函数时底层调用的操作系统提供给用户的读写API调用这些API时会生成对应的指令CPU则会执行这些指令。在计算机刚出现的那段时间所有读写请求的指令都有CPU去执行过多的读写请求会导致CPU无法去执行其他命令从而CPU的利用率降低。 后来DMA(Direct Memory Access直接存储器访问)出现了。当IO请求传到计算机底层时DMA会向CPU请求让DMA去处理这些IO操作从而可以让CPU去执行其他指令。DMA处理IO操作时会请求获取总线的使用权。当IO请求过多时会导致大量总线用于处理IO请求从而降低效率 。 于是便有了Channel(通道)Channel相当于一个专门用于IO操作的独立处理器它具有独立处理IO请求的能力当有IO请求时它会自行处理这些IO请求 。 Java Channel 本地文件IO FileChannel 网络IO SocketChanel、ServerSocketChannel用于TCP传输DatagramChannel用于UDP传输  
获得通道的方法 
对象调用getChannel() 方法 
获取通道的一种方式是对支持通道的对象调用getChannel() 方法。支持通道的类如下 
FileInputStreamFileOutputStreamRandomAccessFileDatagramSocketSocketServerSocket 
例子 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.channels.DatagramChannel;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Paths;public class demo2 {public static void main(String[] args) throws IOException {// 本地通道FileInputStream fileInputStream  new FileInputStream(zwt);FileChannel channel1  fileInputStream.getChannel();FileOutputStream fileOutputStream  new FileOutputStream(zwt);FileChannel channel2  fileOutputStream.getChannel();// 网络通道Socket socket  new Socket();SocketChannel channel3  socket.getChannel();ServerSocket serverSocket  new ServerSocket();ServerSocketChannel channel4  serverSocket.getChannel();DatagramSocket datagramSocket  new DatagramSocket();DatagramChannel channel5  datagramSocket.getChannel();// 最后要关闭通道FileChannel open  FileChannel.open(Paths.get(zwt));SocketChannel open1  SocketChannel.open();}
} 
getChannel()非直接缓冲区 
getChannel()获得通道allocate()获得非直接缓冲区 
通过非直接缓冲区读写数据需要通过通道来传输缓冲区里的数据 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;public class demo4 {public static void main(String[] args) {FileInputStream is  null;FileOutputStream os  null;// 获得通道FileChannel inChannel  null;FileChannel outChannel  null;// 利用 try-catch-finally 保证关闭try {is  new FileInputStream();os  new FileOutputStream();// 获得通道inChannel  is.getChannel();outChannel  os.getChannel();// 获得缓冲区用于在通道中传输数据ByteBuffer byteBuffer  ByteBuffer.allocate(1024);// 循环将字节数据放入到buffer中然后写入磁盘中while (inChannel.read(byteBuffer) ! -1) {// 切换模式byteBuffer.flip();outChannel.write(byteBuffer);byteBuffer.clear();}} catch (IOException e) {e.printStackTrace();} finally {if (inChannel ! null) {try {inChannel.close();} catch (IOException e) {e.printStackTrace();}}if (outChannel ! null) {try {outChannel.close();} catch (IOException e) {e.printStackTrace();}}if (is ! null) {try {is.close();} catch (IOException e) {e.printStackTrace();}}if (os ! null) {try {os.close();} catch (IOException e) {e.printStackTrace();}}}}
} 
open()直接缓冲区 
通过open获得通道通过FileChannel.map()获取直接缓冲区 
使用直接缓冲区时无需通过通道来传输数据直接将数据放在缓冲区内即可 
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;public class demo5 {public static void main(String[] args) throws IOException {// 通过open()方法来获得通道FileChannel inChannel  FileChannel.open(Paths.get(), StandardOpenOption.READ);// outChannel需要为 READ WRITE CREATE模式// READ WRITE是因为后面获取直接缓冲区时模式为READ_WRITE模式// CREATE是因为要创建新的文件FileChannel outChannel  FileChannel.open(Paths.get(), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);// 获得直接缓冲区MappedByteBuffer inMapBuf  inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());MappedByteBuffer outMapBuf  outChannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size());// 字节数组byte[] bytes  new byte[inMapBuf.limit()];// 因为是直接缓冲区可以直接将数据放入到内存映射文件无需通过通道传输inMapBuf.get(bytes);outMapBuf.put(bytes);// 关闭缓冲区这里没有用try-catch-finallyinChannel.close();outChannel.close();}
} 
通道间直接传输 
public static void channelToChannel() throws IOException {long start  System.currentTimeMillis();// 通过open()方法来获得通道FileChannel inChannel  FileChannel.open(Paths.get(), StandardOpenOption.READ);// outChannel需要为 READ WRITE CREATE模式// READ WRITE是因为后面获取直接缓冲区时模式为READ_WRITE模式// CREATE是因为要创建新的文件FileChannel outChannel  FileChannel.open(Paths.get(), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);// 通道间直接传输inChannel.transferTo(0, inChannel.size(), outChannel);// 对应的还有transferFrom// outChannel.transferFrom(inChannel, 0, inChannel.size());inChannel.close();outChannel.close();
} 
直接缓冲区VS非直接缓冲区 
// getChannel()  非直接缓冲区耗时
708
// open()  直接缓冲区耗时
115
// channel transferTo channel耗时
47直接缓冲区的读写速度虽然很快但是会占用很多很多内存空间。如果文件过大会使得计算机运行速度变慢 
分散和聚集 
分散读取 
分散读取Scattering Reads是指从Channel 中读取的数据“分散”到多个Buffer 中。 
注意按照缓冲区的顺序从Channel 中读取的数据依次将 Buffer 填满。 
聚集写入 
聚集写入Gathering Writes是指将多个Buffer 中的数据“聚集”到Channel。 
按照缓冲区的顺序写入position 和limit 之间的数据到Channel。 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;public class demo6 {public static void main(String[] args) throws IOException {FileInputStream is  new FileInputStream();FileOutputStream os  new FileOutputStream();FileChannel inChannel  is.getChannel();FileChannel outChannel  os.getChannel();// 获得多个缓冲区并且放入到缓冲区数组中ByteBuffer byteBuffer1  ByteBuffer.allocate(50);ByteBuffer byteBuffer2  ByteBuffer.allocate(1024);ByteBuffer[] byteBuffers  {byteBuffer1, byteBuffer2};// 分散读取inChannel.read(byteBuffers);byteBuffer1.flip();byteBuffer2.flip();// 聚集写入outChannel.write(byteBuffers);}
} 
非阻塞式网络通信 
概念 
底层原理可见操作系统-文件IO 
比喻 
举个你去饭堂吃饭的例⼦你好⽐⽤户程序饭堂好⽐操作系统。阻塞 I/O 好⽐
你去饭堂吃饭但是饭堂的菜还没做好然后你就⼀直在那⾥等啊等等了好⻓⼀段时间终于等到饭堂阿姨把菜端了出来数据准备的过程但是你还得继续等阿姨把菜内核空间打到你的饭盒⾥⽤户空间经历完这两个过程你才可以离开。⾮阻塞 I/O 好⽐
你去了饭堂问阿姨菜做好了没有阿姨告诉你没你就离开了过⼏⼗分钟你⼜来饭堂问阿姨阿姨说做好了于是阿姨帮你把菜打到你的饭盒⾥这个过程你是得等待的。基于⾮阻塞的 I/O 多路复⽤好⽐
你去饭堂吃饭发现有⼀排窗⼝饭堂阿姨告诉你这些窗⼝都还没做好菜等做好了再通知你于是等啊等 select 调⽤中过了⼀会阿姨通知你菜做好了但是不知道哪个窗⼝的菜做好了你⾃⼰看吧。于是你只能⼀个⼀个窗⼝去确认后⾯发现 5 号窗⼝菜做好了于是你让 5 号窗⼝的阿姨帮你打菜到饭盒⾥这个打菜的过程你是要等待的虽然时间不⻓。打完菜后你⾃然就可以离开了。异步 I/O 好⽐
你让饭堂阿姨将菜做好并把菜打到饭盒⾥后把饭盒送到你⾯前整个过程你都不需要任何等待。 
阻塞式网络通信 
package NIOAndBIO;import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;public class BIO {public static void main(String[] args) throws IOException {Thread thread1  new Thread(() - {try {server();} catch (IOException e) {e.printStackTrace();}});Thread thread2  new Thread(() - {try {client();} catch (IOException e) {e.printStackTrace();}});thread1.start();thread2.start();}public static void client() throws IOException {// 创建客户端通道SocketChannel socketChannel  SocketChannel.open(new InetSocketAddress(127.0.0.1, 2022));// 读取信息 D:\\bizhi\\bizhi202008\\wallhaven-kwp2qq.jpgFileChannel fileChannel  FileChannel.open(Paths.get(D:\\\\bizhi\\\\bizhi202008\\\\wallhaven-kwp2qq.jpg), StandardOpenOption.READ);// 创建缓冲区ByteBuffer byteBuffer  ByteBuffer.allocate(1024);// 写入数据while (fileChannel.read(byteBuffer) ! -1) {byteBuffer.flip();socketChannel.write(byteBuffer);byteBuffer.clear();}fileChannel.close();socketChannel.close();}public static void server() throws IOException {// 创建服务端通道ServerSocketChannel serverSocketChannel  ServerSocketChannel.open();FileChannel fileChannel  FileChannel.open(Paths.get(D:\\\\bizhi\\\\bizhi202008\\\\wallhaven-kwp2qq.jpg), StandardOpenOption.WRITE, StandardOpenOption.CREATE);// 绑定链接serverSocketChannel.bind(new InetSocketAddress(2022));// 获取客户端的通道SocketChannel socketChannel  serverSocketChannel.accept();// 创建缓冲区ByteBuffer byteBuffer  ByteBuffer.allocate(1024);while (socketChannel.read(byteBuffer) ! -1) {byteBuffer.flip();fileChannel.write(byteBuffer);byteBuffer.clear();}socketChannel.close();fileChannel.close();serverSocketChannel.close();}
} 
非阻塞式网络通信 
package NIOAndBIO;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.Iterator;
import java.util.Scanner;public class NIO {public static void main(String[] args) {Thread thread1  new Thread(()-{try {server();} catch (IOException e) {e.printStackTrace();}});Thread thread2  new Thread(()-{try {client();} catch (IOException e) {e.printStackTrace();}});thread1.start();thread2.start();}public static void client() throws IOException {SocketChannel socketChannel  SocketChannel.open(new InetSocketAddress(127.0.0.1, 2020));// 设置为非阻塞模式socketChannel.configureBlocking(false);ByteBuffer byteBuffer  ByteBuffer.allocate(1024);Scanner scanner  new Scanner(System.in);while (scanner.hasNext()) {String str  scanner.next();byteBuffer.put(str.getBytes());byteBuffer.flip();socketChannel.write(byteBuffer);byteBuffer.clear();}byteBuffer.clear();socketChannel.close();}public static void server() throws IOException {ServerSocketChannel serverSocketChannel  ServerSocketChannel.open();serverSocketChannel.configureBlocking(false);serverSocketChannel.bind(new InetSocketAddress(2020));// 获得选择器Selector selector  Selector.open();// 将通道注册到选择器中设定为接收操作serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);// 轮询接受while (selector.select()  0) {IteratorSelectionKey iterator  selector.selectedKeys().iterator();// 获得事件的keywhile (iterator.hasNext()) {SelectionKey key  iterator.next();if (key.isAcceptable()) {SocketChannel socketChannel  serverSocketChannel.accept();socketChannel.configureBlocking(false);socketChannel.register(selector, SelectionKey.OP_READ);} else if (key.isReadable()) {// 从选择器中获取通道SocketChannel socketChannel  (SocketChannel) key.channel();ByteBuffer byteBuffer  ByteBuffer.allocate(10);while (socketChannel.read(byteBuffer) ! -1) {int len  byteBuffer.limit();byteBuffer.flip();System.out.println(new String(byteBuffer.array(), 0, len));byteBuffer.clear();}socketChannel.close();}iterator.remove();}}serverSocketChannel.close();}
} 
选择器 
选择器Selector是SelectableChannle 对象的多路复用器Selector 可以同时监控多个SelectableChannel 的IO 状况也就是说利用Selector 可使一个单独的线程管理多个Channel。Selector 是非阻塞IO 的核心 。 选择器的创建 
// 创建一个选择器
Selector selector  Selector.open(); 
绑定选择器 
通过调用通道的register方法可以绑定选择器register方法有两个参数 
Selector即绑定哪个选择器ops监听事件类型。ops有4个值可以选择为SelectionKey的静态属性 
// 让选择器监听一种状态
myChannel.register(selector, SelectionKey.OP_READ);
// 让选择器监听多种状态
myChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_ACCEPT); 
SelectionKey 
表示SelectableChannel 和Selector 之间的注册关系。每次向选择器注册通道时就会选择一个事件(选择键)。选择键包含两个表示为整数值的操作集。操作集的每一位都表示该键的通道所支持的一类可选择操作。