版权声明:本文为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会立即执行完成并且返回数据,这样底层就会给用户态数据,然后结束阻塞