13.NIO 摘要

NIO 相关类都被放在 java.nio 包及子包下, 并对原 java.io 包中的很多类进行改写.

NIO 有三大核心部分: Channel 通道, Buffer 缓冲区, Selector 缓冲区.

通道可以理解为 BIO 中的 Socket 连接, 每个通道又对应一个缓存区, 在读/写数据时都是针对缓冲区进行操作.

NIO 是面向缓冲区或者面向块编程(数据都是按照块进行组织和管理). 数据读取到缓冲区, 需要时可以在缓冲区中前后移动, 这就增加了处理过程中的灵活性, 使用它可以提供非阻塞式的高伸缩网络.

Java NIO 的非阻塞模式, 使一个线程从某通道发送请求或者读取数据, 但是它仅能得到目前可用的数据, 如果目前没有数据可用时, 就什么都不会获取, 而不是保持线程阻塞, 所以直至数据变得可以读取之前, 该线程可以继续做其它的事情. 非阻塞写也是如此, 一个线程请求写入一些数据到某通道, 但不需要等待完全写入, 这个线程同时可以做别的事情.

通俗理解: NIO 是可以做到用一个线程来处理多个操作的. 假设有 10000 个请求过来, 根据实际情况, 可以分配 50 或 1000 个线程来处理. 不像之前的阻塞 IO 那样, 非得分配 10000 个.

举例说明 Buffer 使用

public class BasicBuffer {
    public static void main(String[] args) {
        // 创建一个 Buffer, 大小为 10, 即可以存放 10 个 int
        IntBuffer intBuffer = IntBuffer.allocate(10);

        // 向 Buffer 中存放数据.
        intBuffer.put(1);
        intBuffer.put(2);
        intBuffer.put(3);
        intBuffer.put(4);
        intBuffer.put(5);

        // 进行读写切换
        intBuffer.flip();

        // 从 Buffer 读取数据.
        while (intBuffer.hasRemaining()) {
            System.out.println(intBuffer.get());
        }
    }
}

服务端

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
import java.util.Set;

public class NIOServer {
    public static void main(String[] args) throws Exception {

        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

        Selector selector = Selector.open();
        //serverSocketChannel.socket().bind(new InetSocketAddress(6666));
        serverSocketChannel.bind(new InetSocketAddress(6666));

        serverSocketChannel.configureBlocking(false);
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        while (true) {
            if (selector.select(1000) == 0) {
                continue;
            }

            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            Iterator<SelectionKey> iterator = selectionKeys.iterator();
            while (iterator.hasNext()) {
                SelectionKey selectionKey = iterator.next();
                if(selectionKey.isAcceptable()) {
                    System.out.println("~~~~~~~~~~~~~");
                    SocketChannel socketChannel = serverSocketChannel.accept();
                    if (socketChannel != null){
                        socketChannel.configureBlocking(false);
                        socketChannel.register(selector, SelectionKey.OP_READ);
                    }
                }

                if (selectionKey.isReadable()) {
                    System.out.println("++++++++++++++++");
                    SocketChannel channel = (SocketChannel) selectionKey.channel();

                    ByteBuffer src = ByteBuffer.allocate(1024);
                    channel.read(src);
                    System.out.println(new String(src.array()));
                }

                iterator.remove();
            }
        }
    }
}

讨论

不需要等待完全写入是什么意思?
FileOutputStream 举例, 调用 write 方法写数据时, 会将数据写到对应文件中.
在没有将数据写完之前 write 方法, 是无法返回, 这时当前线程是无法做其它事情的.

后面会说关于 NIO 方面的实现.

参考文章

socket()函数介绍

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值