Selector#wakeup()
看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();
}
}; //task
Thread 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()给线程发个中断信号实现的
Selector#wakeup()的更多相关文章
- Java NIO之选择器Selector
在单独的线程中,检查多个通道是否可以进行IO操作. Selector创建:静态工厂方法创建 Selector selector = Selector.open(); 注册通道 channel.conf ...
- java的nio之:java的nio系列教程之selector
一:Java NIO的selector的概述===>Selector(选择器)是Java NIO中能够检测一到多个NIO通道,并能够知晓通道是否为诸如读写事件做好准备的组件.这样,一个单独的线程 ...
- Java基础知识强化之IO流笔记77:NIO之 Selector
Selector(选择器)是Java NIO中能够检测一到多个NIO通道,并能够知晓通道是否为诸如读写事件做好准备的组件.这样,一个单独的线程可以管理多个channel,从而管理多个网络连接. 1. ...
- Java NIO类库Selector机制解析(下)
五. 迷惑不解 : 为什么要自己消耗资源? 令人不解的是为什么我们的Java的New I/O要设计成这个样子?如果说老的I/O不能多路复用,如下图所示,要开N多的线程去挨个侦听每一个Channel ...
- NIO组件Selector详解
Selector(选择器)是Java NIO中能够检测一到多个NIO通道,并能够知晓通道是否为诸如读写事件做好准备的组件.这样,一个单独的线程可以管理多个channel,从而管理多个网络连接. 下面是 ...
- NIO组件Selector工作机制详解(下)
转自:http://blog.csdn.net/haoel/article/details/2224069 五. 迷惑不解 : 为什么要自己消耗资源? 令人不解的是为什么我们的Java的New I/ ...
- Java NIO——Selector机制源码分析---转
一直不明白pipe是如何唤醒selector的,所以又去看了jdk的源码(openjdk下载),整理了如下: 以Java nio自带demo : OperationServer.java Oper ...
- Java NIO类库Selector机制解析--转
一. 前言 自从J2SE 1.4版本以来,JDK发布了全新的I/O类库,简称NIO,其不但引入了全新的高效的I/O机制,同时,也引入了多路复用的异步模式.NIO的包中主要包含了这样几种抽象数据类型: ...
- NIO(四、Selector)
目录 NIO(一.概述) NIO(二.Buffer) NIO(三.Channel) NIO(四.Selector) Selector 前面两个章节都描述了Buffer和Channel,那这个章节就描述 ...
随机推荐
- loj #6570. 毛毛虫计数
$ \color{#0066ff}{ 题目描述 }$ hsezoi 巨佬 olinr 喜欢 van 毛毛虫,他定义毛毛虫是一棵树,满足树上存在一条树链,使得树上所有点到这条树链的距离最多为 1. 给定 ...
- To 高一
Linux 坑待填 Special Judge 什么是 Special Judge?有的题目会让你输出任意一个解即可AC,或者是让你输出部分答案能取得本测试点部分得分,或者是按照方案的优秀程度给你分, ...
- 使用cookie下次自动登录
登录时勾选了自动登录处理: 1.加密账号和IP,保存在cookie中,cookie('auto', $value, $time) 2.解密cookie,取出账号和上次IP,判断上次IP==当前IP.账 ...
- 2018年10月19 手记 - 身为开发者的我de窘境
从10月1国庆过完节回来,那已经是7号了,之后便开始紧锣密鼓的筹划着接下来11月份的公司组织的对外活动,这边新来的产品对产品或者说对任务很是负责,并且策划了很多的方案,并且乐意站在我们开发的角度上去考 ...
- (一)Win消息机制,SDK编程基础
一,基本概念 SDK:软件开发工具包(缩写:SDK.外语全称:Software Development Kit) API:应用程序接口 WINAPI:Windows平台下的系统调用,包含 window ...
- 洛谷 P3201 [HNOI2009]梦幻布丁(启发式合并)
题面 luogu 题解 什么是启发式合并? 小的合并到大的上面 复杂度\(O(nlogn)\) 这题颜色的修改,即是两个序列的合并 考虑记录每个序列的\(size\) 小的合并到大的 存序列用链表 但 ...
- Django项目创建与管理
1.主题 这部分教程主要介绍如何通过Pycharm创建.管理.运行一个Django工程.对于Django模块的相关知识大家可以参考Python社区. 2.准备环境 Django版本为2.0或更高 Py ...
- dedicated bearer
Session Initiation Protocol (SIP) Basic Call Flow ExamplesRFC3665 intuitive, done. PDF] TS 123 401 ...
- 【车联网系统】基于Python实现的现网全自动化测试方案
前面写了几篇基于CANoe实现的仿真测试台架,属于半自动化测试方案.现出一个Python实现的车联网系统全自动化测试方案. 目录结构 1.-----------------------车联网系统组成 ...
- docker jenkins使用(二)
jenkins的安装很简单,但是jenkins的初次使用却很头疼.对于小白来说有点不太明白 背景: 开发更新app需要很多步骤,生成jar包.上传服务器.更新启动程序,如果有很多服务器,那么需要做好多 ...