java nio wakeup_java nio的reactor模型中selector的wakeup方法有什么用?

这个可以从wakeup()的注释上找到答案:

/**

* Causes the first selection operation that has not yet returned to return

* immediately.

*

*

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.

*

*

Invoking this method more than once between two successive selection

* operations has the same effect as invoking it just once.

*

* @return This selector

*/

public abstract Selector wakeup();当B线程阻塞在select()或select(long)方法上时,A线程调用wakeup后,B线程会立刻返回。

如果没有线程阻塞在select()方法上,那么下一次某个线程调用select()或select(long)方法时,会立刻返回。

所以,当在Handler中完成SocketChannel注册后,显示的调用selector.wakeup()方法,虽然当前没有阻塞在select()上,但是会影响下一次调用select()。为什么在这里需要调用wakeup()方法?当注册新的Channel或事件时,希望能及时处理。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值