Selector的wakeup()

本文深入解析Java NIO中Selector的wakeup()方法,揭示其如何唤醒阻塞的select()操作,使线程立即返回并确保返回值不为零。探讨了在无进行中的选择操作时,该方法如何在下次select调用时生效。

版权声明:本文为优快云博主「木易九日111」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/weixin_44601714/article/details/100081131

/**
 * Causes the first selection operation that has not yet returned to return
 * immediately.
 *
 * <p> If another thread is currently blocked in an invocation of the
 * {@link #select()} or {@link #select(long)} methods then that invocation
 * will return immediately.  If no selection operation is currently in
 * progress then the next invocation of one of these methods will return
 * immediately unless the {@link #selectNow()} method is invoked in the
 * meantime.  In any case the value returned by that invocation may be
 * non-zero.  Subsequent invocations of the {@link #select()} or {@link
 * #select(long)} methods will block as usual unless this method is invoked
 * again in the meantime.
 *
 * <p> Invoking this method more than once between two successive selection
 * operations has the same effect as invoking it just once.  </p>
 *
 * @return  This selector
 */
 翻译一下,就是说

其他线程如果因为调用了selector.select()或者selector.select(long)这两个方法而阻塞,
调用了selector.wakeup()之后,就会立即返回结果,并且返回的值!=0,
如果当前Selector没有阻塞在select方法上,
那么本次 wakeup调用会在下一次select阻塞的时候生效。
public abstract Selector wakeup();

也就是说,这个方法是用来唤醒阻塞的select()方法的

我们知道select()方法会阻塞,是因为用户态将socket的文件描述符和感兴趣的事件传递给操作系统底层的pipe,底层函数执行完成,触发事件之后底层就会向用户态返回数据,这样才会打破阻塞,
在这里插入图片描述

wakeup是如何唤醒阻塞的select()呢,我们从源码角度来看

public Selector wakeup() {
    synchronized(this.interruptLock) {
        if (!this.interruptTriggered) {
            this.setWakeupSocket();
            this.interruptTriggered = true;
        }

        return this;
    }
}
//这里追到了naive方法
private native void setWakeupSocket0(int var1);

setWakeupSocket()方法 会直接向pipe中添加wakeupSinkFD,这个FD会立即执行完成并且返回数据,这样底层就会给用户态数据,然后结束阻塞

Java NIO Selector 是实现高效非阻塞 I/O 操作的重要组件,它允许单个线程处理多个通道(Channel),从而显著提升网络服务器的性能和资源利用率。Selector 的核心功能是监控多个 Channel 的 I/O 事件(如可读、可写等),并在事件就绪时通知应用程序进行处理。 ### Selector 的基本使用步骤 1. **创建 Selector 对象** 通过 `Selector.open()` 方法打开一个 Selector 实例。这个实例将用于注册 Channel 并监听其 I/O 事件。 ```java Selector selector = Selector.open(); ``` 2. **创建并注册 Channel 到 Selector** 通常使用 `SocketChannel` 或 `ServerSocketChannel`,将其设置为非阻塞模式后,注册到 Selector 上,并指定感兴趣的事件类型(如 `OP_READ`, `OP_WRITE`, `OP_CONNECT`, `OP_ACCEPT`)。 ```java ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(false); serverSocketChannel.bind(new InetSocketAddress(8080)); serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); ``` 3. **轮询就绪事件** 使用 `selector.select()` 方法阻塞等待至少一个 Channel 的事件就绪。该方法返回值表示就绪的 Channel 数量。也可以使用 `select(timeout)` 设置超时时间。 ```java int readyChannels = selector.select(); ``` 4. **处理就绪事件** 获取 `selectedKeys()` 集合,遍历所有就绪的 `SelectionKey`,根据其事件类型执行相应的 I/O 操作。例如: ```java Set<SelectionKey> selectedKeys = selector.selectedKeys(); Iterator<SelectionKey> keyIterator = selectedKeys.iterator(); while (keyIterator.hasNext()) { SelectionKey key = keyIterator.next(); if (key.isAcceptable()) { // 处理客户端连接 ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel(); SocketChannel clientChannel = serverChannel.accept(); clientChannel.configureBlocking(false); clientChannel.register(selector, SelectionKey.OP_READ); } else if (key.isReadable()) { // 处理读取操作 SocketChannel clientChannel = (SocketChannel) key.channel(); ByteBuffer buffer = ByteBuffer.allocate(256); int read = clientChannel.read(buffer); if (read == -1) { clientChannel.close(); } else { buffer.flip(); System.out.println("Received: " + new String(buffer.array())); } } keyIterator.remove(); } ``` 5. **关闭 Selector** 当不再需要时,调用 `selector.close()` 关闭 Selector,释放相关资源。 ### Selector 的优势与适用场景 Selector 的最大优势在于其高效的 I/O 多路复用机制,使得一个线程可以同时管理多个 Channel,避免了为每个连接分配一个线程所带来的资源浪费和上下文切换开销。这种机制特别适合高并发、大量连接的网络服务,如 Web 服务器、聊天服务器等。 此外,Selector 还支持多种事件类型,包括: - `OP_ACCEPT`:用于监听新连接 - `OP_READ`:用于监听可读事件 - `OP_WRITE`:用于监听可写事件 - `OP_CONNECT`:用于客户端连接完成事件 ### 注意事项 - 在注册 Channel 到 Selector 时,必须确保其处于非阻塞模式。 - 每次处理完一个 `SelectionKey` 后,应将其从 `selectedKeys` 中移除,防止重复处理。 - `select()` 方法可能被外部中断或超时,需根据实际情况进行处理。 - 在高并发环境下,建议使用 `Selector.wakeup()` 方法唤醒阻塞的 `select()` 调用,以便及时处理新的事件。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值