非阻塞式 I/O 模型的工作原理【NIO】

非阻塞式 I/O(Non-blocking I/O,NIO)是一种改进的 I/O 模型,引入了通道(Channel)和缓冲区(Buffer)的概念。相比于阻塞式 I/O,非阻塞式 I/O 允许在进行读/写操作时不会导致线程阻塞,而是立即返回,如果数据没有准备好,方法会返回一个特殊的值(通常是零),表示操作无法立即完成。在非阻塞式 I/O 中,一个线程可以管理多个连接,通过选择器(Selector)实现多路复用,从而提高了系统的并发能力和性能。

特点:

  1. 通道和缓冲区:非阻塞式 I/O 引入了通道和缓冲区的概念,通过通道进行数据的读写,而不是直接对流进行操作,这样可以提高效率。

  2. 选择器(Selector):非阻塞式 I/O 使用选择器实现多路复用,一个线程可以同时管理多个通道,当有数据可读写时,选择器会通知线程进行相应的操作,避免了线程阻塞。

  3. 立即返回:非阻塞式 I/O 的读/写操作不会导致线程阻塞,如果数据没有准备好,方法会立即返回,可以继续执行其他任务,提高了系统的并发能力。

适用场景:

非阻塞式 I/O 适用于高并发、大量连接的场景,能够提高系统的吞吐量和性能。例如,网络服务器、聊天程序、实时数据传输等场景都可以使用非阻塞式 I/O 来实现。

示例:

下面是一个简单的使用非阻塞式 I/O 的示例,使用 Java NIO 实现一个简单的 Echo 服务器,即将客户端发送的消息原样返回给客户端:

复制代码
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.Set;

public class NonBlockingServer {
    public static void main(String[] args) throws IOException {
        // 创建 Selector
        Selector selector = Selector.open();

        // 创建 ServerSocketChannel 并绑定端口
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.bind(new InetSocketAddress(8080));
        serverSocketChannel.configureBlocking(false);

        // 将 ServerSocketChannel 注册到 Selector 上,并指定监听事件为接受连接事件
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        System.out.println("Server started, listening on port 8080...");

        while (true) {
            // 阻塞等待事件发生
            selector.select();

            // 获取发生事件的 SelectionKey 集合
            Set<SelectionKey> selectedKeys = selector.selectedKeys();
            Iterator<SelectionKey> iterator = selectedKeys.iterator();

            // 遍历处理事件
            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();
                iterator.remove();

                // 处理接受连接事件
                if (key.isAcceptable()) {
                    ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();
                    SocketChannel clientChannel = serverChannel.accept();
                    clientChannel.configureBlocking(false);
                    clientChannel.register(selector, SelectionKey.OP_READ);
                    System.out.println("Client connected: " + clientChannel.getRemoteAddress());
                }

                // 处理读事件
                if (key.isReadable()) {
                    SocketChannel clientChannel = (SocketChannel) key.channel();
                    ByteBuffer buffer = ByteBuffer.allocate(1024);
                    int bytesRead = clientChannel.read(buffer);
                    if (bytesRead != -1) {
                        buffer.flip();
                        byte[] bytes = new byte[buffer.remaining()];
                        buffer.get(bytes);
                        String message = new String(bytes);
                        System.out.println("Received message from client: " + message);

                        // 原样返回给客户端
                        clientChannel.write(ByteBuffer.wrap(bytes));
                    } else {
                        // 客户端关闭连接
                        key.cancel();
                        clientChannel.close();
                        System.out.println("Client disconnected: " + clientChannel.getRemoteAddress());
                    }
                }
            }
        }
    }
}

在这个示例中,我们首先创建了一个 Selector 对象,然后创建了一个 ServerSocketChannel 并绑定到指定端口,将其注册到 Selector 上,并指定监听事件为接受连接事件。在循环中,调用 select() 方法阻塞等待事件发生,一旦有事件发生,通过遍历 selectedKeys 处理相应的事件。当有客户端连接进来时,将客户端的 SocketChannel 注册到 Selector 上,并指定监听事件为读事件。在读事件发生时,从 SocketChannel 中读取数据,然后将数据原样返回给客户端。

相关推荐
好望角雾眠2 分钟前
第四阶段C#通讯开发-6:Socket之UDP
开发语言·笔记·学习·udp·c#
升鲜宝供应链及收银系统源代码服务10 分钟前
升鲜宝生鲜配送供应链管理系统---PMS--商品品牌多语言存储与 Redis 缓存同步实现
java·开发语言·数据库·redis·缓存·开源·供应链系统
大佬,救命!!!12 分钟前
python实现象棋
开发语言·python·学习笔记·pygame·少儿编程·记录成长
棉猴13 分钟前
《pygame中Sprite类实现多帧动画》注-通过多张序列帧显示动画2-2
开发语言·python·游戏·游戏程序·pygame
练习时长一年21 分钟前
Spring AoP的切点匹配
java·开发语言
霜绛1 小时前
C#知识补充(一)——ref和out、成员属性、万物之父和装箱拆箱、抽象类和抽象方法、接口
开发语言·笔记·学习·c#
T.Ree.1 小时前
cpp_list
开发语言·数据结构·c++·list
laocooon5238578861 小时前
C++ 图片加背景音乐的处理
开发语言·c++
爱编程的鱼2 小时前
C# var 关键字详解:从入门到精通
开发语言·c#·solr
MATLAB代码顾问2 小时前
MATLAB实现TCN神经网络数值预测
开发语言·matlab