Selector的wakeup()

版权声明:本文为CSDN博主「木易九日111」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/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会立即执行完成并且返回数据,这样底层就会给用户态数据,然后结束阻塞

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值