看thrift源码发现selector.wakeup()方法,通常在selector.select()后线程会阻塞。使用wakeup()方法,线程会立即返回。源码分析应该是用的线程中断实现的。下面是个小demo

public class TestSelector {private static Selector selector;public static void main(String[] args) throws IOException, InterruptedException {startSelectorThread();Thread.sleep(2000);selector.wakeup();}private static void startSelectorThread(){Runnable selectorTask = () -> {try{String host = "127.0.0.1";int port = 8888;ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();serverSocketChannel.configureBlocking(false);SocketAddress socketAddress = new InetSocketAddress(host, port);serverSocketChannel.bind(socketAddress);selector = Selector.open();while(true){serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);System.out.println("selector start select .....");Set<SelectionKey> selectionKeySet =  selector.selectedKeys();for(Iterator<SelectionKey> iterator = selectionKeySet.iterator(); iterator.hasNext(); ){SelectionKey selectionKey = iterator.next();System.out.println(selectionKey.toString());selectionKeySet.remove(selectionKey);ServerSocketChannel serverSocketChannel1 = (ServerSocketChannel) selectionKey.channel();SocketChannel clientChannel = serverSocketChannel1.accept();clientChannel.configureBlocking(false);clientChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);iterator.remove();}System.out.println("selector end select ....");}}catch (Exception e){e.printStackTrace();}}; //taskThread thread = new Thread(selectorTask);thread.start();}
}

我们看下wakeup()注释

/*** 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*/public abstract Selector wakeup();

可以看出,这个方法会让阻塞的线程立即返回。跟进poll实现的selector的wakeup()方法

public Selector wakeup() {Object var1 = this.interruptLock;synchronized(this.interruptLock) {if (!this.interruptTriggered) {this.pollWrapper.interrupt();this.interruptTriggered = true;}return this;}}

上面可以看出使用的是interrupt()给线程发个中断信号实现的

转载于:https://www.cnblogs.com/luckygxf/p/9395297.html

Selector#wakeup()相关推荐

  1. Selector的wakeup()

    版权声明:本文为CSDN博主「木易九日111」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明. 原文链接:https://blog.csdn.net/weixin ...

  2. Java NIO系列教程(六) Selector

    Selector(选择器)是Java NIO中能够检测一到多个NIO通道,并能够知晓通道是否为诸如读写事件做好准备的组件.这样,一个单独的线程可以管理多个channel,从而管理多个网络连接. 下面是 ...

  3. java nio Selector (新IO)分析

    Selector(选择器)是Java NIO中能够检测一到多个NIO通道,并能够知晓通道是否为诸如读写事件做好准备的组件.这样,一个单独的线程可以管理多个channel,从而管理多个网络连接. 为什么 ...

  4. 【Netty】NIO 选择器 ( Selector ) 简介

    文章目录 I . 选择器 ( Selector ) II . 选择器 ( Selector ) 与 NIO 特性 III . 选择器 ( Selector ) API 简介 IV . Selectio ...

  5. NIO详解(九):Selector详解

    1. 概述 Selector(选择器)是Java NIO中能够检测一到多个NIO通道,并能够知晓通道是否为诸如读写事件做好准备的组件.这样,一个单独的线程可以管理多个channel,从而管理多个网络连 ...

  6. Java NIO——Selector机制源码分析---转

    一直不明白pipe是如何唤醒selector的,所以又去看了jdk的源码(openjdk下载),整理了如下: 以Java nio自带demo : OperationServer.java   Oper ...

  7. Java NIO类库Selector机制解析--转

    一.  前言 自从J2SE 1.4版本以来,JDK发布了全新的I/O类库,简称NIO,其不但引入了全新的高效的I/O机制,同时,也引入了多路复用的异步模式.NIO的包中主要包含了这样几种抽象数据类型: ...

  8. nio的epoll和selector实现流程分析

    一.NETTY底层使用的是NIO的selector和epoll进行实现的,select,poll,epoll都是IO多路复用的机制.I/O多路复用就是通过一种机制,一个进程可以监视多个描述符,一旦某个 ...

  9. Selector SelectionKey

    NIO能通过单个线程管理多个I/O通道,主要就是通过选择器Selector来实现的. public abstract class Selector {protected Selector() { }p ...

最新文章

  1. pymatgen读/写各种文件
  2. 50+篇《神经架构搜索NAS》2020论文合集
  3. 面经 cisco 2
  4. 移动互联网时代: 妈,我回不去了
  5. 452 Minimum Number of Arrows to Burst Balloons
  6. Spring5参考指南:基于注解的容器配置
  7. 大话设计模式笔记 享元模式
  8. http响应状态码列表
  9. JS 的平凡之路--学习人气眼中的效果(上)
  10. source insight 配置
  11. 手动修改VirtrualBox虚拟机的操作系统时间——WinXP为例
  12. 韩语在线翻译图片识别_超强的免费OCR文字扫描工具,网页视频PDF均可识别并翻译...
  13. 2023王道C语言训练营(二叉查找树-顺序查找-折半查找)
  14. unity实现游戏帧同步之确定性物理引擎
  15. 【VRP问题】基于帝国企鹅优化算法求解冷链配送物流车辆调度优化研究(Matlab代码实现)
  16. 从京东618秒杀聊聊秒杀限流的多种实现!
  17. 怎么把分开的pdf放在一起_怎么把合并的pdf文件拆分?
  18. Arduino与人体感应模块
  19. AWS ELB技术要点
  20. python读取bin文件,转成16进制通信协议

热门文章

  1. 计算机视觉论文-2021-07-01
  2. 【深度学习系列】——深度学习简介
  3. 计算机视觉 | Python OpenCV 3 使用背景减除进行目标检测
  4. ResNet详解(转)
  5. angular中如何定义全局变量_如何在Angular 2 / Typescript中声明全局变量?
  6. 刑事科学技术专业与计算机专业,辽宁警察学院刑事科学技术专业
  7. netapp脚本保存日志_Shell脚本实战:日志关键字监控+自动告警
  8. java spring配置文件路径_java - 在spring b中从命令行设置活动配置文件和配置位置...
  9. hbase 查询_云HBase发布全文索引服务,轻松应对复杂查询
  10. ACM-Maximum Tape Utilization Ratio