NIO是JDK 1.4 开始有的,其目的是为了提高速度。NIO翻译成no-blocking io。
    传统IO是⼀次⼀个字节地处理数据,NIO是以块(缓冲区)的形式处理数据。最主要的是,NIO可以实现
    非阻塞,而传统IO只能是阻塞的。
    IO的实际场景是文件IO和网络IO,NIO在网络络IO场景下提升就尤其明显了。
    在Java NIO有三个核心部分组成。分别是Buffer(缓冲区)、Channel(管道)以及Selector(选择器)
    候选者:可以简单的理解为:Buffer是存储数据的地方,Channel是运输数据的载体,而Selector用于检查多个
    Channel的状态变更情况

    1. public class NoBlockServer {
    2. public static void main(String[] args) throws IOException {
    3. // 1.获取通道
    4. ServerSocketChannel server = ServerSocketChannel.open();
    5. // 2.切换成⾮阻塞模式
    6. server.configureBlocking(false);
    7. // 3. 绑定连接
    8. server.bind(new InetSocketAddress(6666));
    9. // 4. 获取选择器
    10. Selector selector = Selector.open();
    11. // 4.1将通道注册到选择器上,指定接收“监听通道”事件
    12. server.register(selector, SelectionKey.OP_ACCEPT);
    13. // 5. 轮训地获取选择器上已“就绪”的事件--->只要select()>0,说明已就绪
    14. while (selector.select() > 0) {
    15. // 6. 获取当前选择器所有注册的“选择键”(已就绪的监听事件)
    16. Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
    17. // 7. 获取已“就绪”的事件,(不同的事件做不同的事)
    18. while (iterator.hasNext()) {
    19. SelectionKey selectionKey = iterator.next();
    20. // 接收事件就绪
    21. if (selectionKey.isAcceptable()) {
    22. // 8. 获取客户端的链接
    23. SocketChannel client = server.accept();
    24. // 8.1 切换成⾮阻塞状态
    25. client.configureBlocking(false);
    26. // 8.2 注册到选择器上-->拿到客户端的连接为了读取通道的数据(监听读就绪事件)
    27. client.register(selector, SelectionKey.OP_READ);
    28. } else if (selectionKey.isReadable()) { // 读事件就绪
    29. // 9. 获取当前选择器读就绪状态的通道
    30. SocketChannel client = (SocketChannel) selectionKey.channel();
    31. // 9.1读取数据
    32. ByteBuffer buffer = ByteBuffer.allocate(1024);
    33. // 9.2得到⽂件通道,将客户端传递过来的图⽚写到本地项⽬下(写模式、没有则创建)
    34. FileChannel outChannel = FileChannel.open(Paths.get("2.png"),
    35. StandardOpenOption.WRITE, StandardOpenOption.CREATE);
    36. while (client.read(buffer) > 0) {
    37. // 在读之前都要切换成读模式
    38. buffer.flip();
    39. outChannel.write(buffer);
    40. // 读完切换成写模式,能让管道继续读取⽂件的数据
    41. buffer.clear();
    42. }
    43. }
    44. // 10. 取消选择键(已经处理过的事件,就应该取消掉了)
    45. iterator.remove();
    46. }
    47. }
    48. }
    49. }
    1. public class NoBlockClient {
    2. public static void main(String[] args) throws IOException {
    3. // 1. 获取通道
    4. SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 6666));
    5. // 1.1切换成⾮阻塞模式
    6. socketChannel.configureBlocking(false);
    7. // 1.2获取选择器
    8. Selector selector = Selector.open();
    9. // 1.3将通道注册到选择器中,获取服务端返回的数据
    10. socketChannel.register(selector, SelectionKey.OP_READ);
    11. // 2. 发送⼀张图⽚给服务端吧
    12. FileChannel fileChannel =
    13. FileChannel.open(Paths.get("X:\\Users\\ozc\\Desktop\\⾯试造⽕箭\\1.png"), StandardOpenOption.READ);
    14. // 3.要使⽤NIO,有了Channel,就必然要有Buffer,Buffer是与数据打交道的呢
    15. ByteBuffer buffer = ByteBuffer.allocate(1024);
    16. // 4.读取本地⽂件(图⽚),发送到服务器
    17. while (fileChannel.read(buffer) != -1) {
    18. // 在读之前都要切换成读模式
    19. buffer.flip();
    20. socketChannel.write(buffer);
    21. // 读完切换成写模式,能让管道继续读取⽂件的数据
    22. buffer.clear();
    23. }
    24. // 5. 轮训地获取选择器上已“就绪”的事件--->只要select()>0,说明已就绪
    25. while (selector.select() > 0) {
    26. // 6. 获取当前选择器所有注册的“选择键”(已就绪的监听事件)
    27. Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
    28. // 7. 获取已“就绪”的事件,(不同的事件做不同的事)
    29. while (iterator.hasNext()) {
    30. SelectionKey selectionKey = iterator.next();
    31. // 8. 读事件就绪
    32. if (selectionKey.isReadable()) {
    33. // 8.1得到对应的通道
    34. SocketChannel channel = (SocketChannel) selectionKey.channel();
    35. ByteBuffer responseBuffer = ByteBuffer.allocate(1024);
    36. // 9. 知道服务端要返回响应的数据给客户端,客户端在这⾥接收
    37. int readBytes = channel.read(responseBuffer);
    38. if (readBytes > 0) {
    39. // 切换读模式
    40. responseBuffer.flip();
    41. System.out.println(new String(responseBuffer.array(), 0, readBytes));
    42. }
    43. }
    44. // 10. 取消选择键(已经处理过的事件,就应该取消掉了)
    45. iterator.remove();
    46. }
    47. }
    48. }
    49. }