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,那这个章节就描述 ...
随机推荐
- HTML-★★★★★JavaScritp简介与语法★★★★★
简介: 1.什么是JavaScript? 它是个脚本语言,作用是使 HTML 页面具有更强的动态和交互性,它需要有宿主文件,它的宿主文件就是html文件. JavaScript 是 Web 的编程语 ...
- JVM理解
在阅读本文之前,先向大家强烈推荐一下周志明的<深入理解Java虚拟机>这本书. 前些天面试了阿里的实习生,问到关于Dalvik虚拟机能不能执行class文件,我当时的回答是不能,但是它执行 ...
- Angular material mat-icon 资源参考_Images
ul,li>ol { margin-bottom: 0 } dt { font-weight: 700 } dd { margin: 0 1.5em 1.5em } img { height: ...
- C. Ayoub and Lost Array Round #533 (Div. 2) 【DP】
一.题面 链接 二.分析 关于这题,两个点. 第一个点,是需要能够分析出$[L,R]$区间的3的余数的个数. 首先,可以得到,$[L,R]$区间内共有$(R-L+1)$个数. 设定余数为0,1,2的为 ...
- HTML学习-02
1.本次学习主要是css和页面的跳转 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...
- 在线词云制作tagxedo
最近在用python制作词云的时候发现了一个更加方便快捷很好玩的词云制作网站 http://www.tagxedo.com/app.html 所以今天就来大致介绍下是怎么使用的 1.先来介绍下tagx ...
- C#文件上传编码乱码
又遇到文件编码乱码的事情,这回稍微有些头绪,但是还是花了很多时间去解决. 场景:上传csv文件,导入到数据库.上传文件的编码不定,需要转成unicode存储. 问题点:需要解决判断上传文件的编码. 关 ...
- python -ConfigParser模块讲解
configParser 模块用于操作配置文件 注:Parser汉译为“解析”之意. 配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键= ...
- 高效的JS数组操作
1.向数组的末尾添加元素 var arr=[1,2,3]; arr[arr.length]=1; 2.向数组的头部添加元素 var arr=[1,2,3]; [0].concat(arr); 3.向数 ...
- Zookeeper概念学习系列之paxos协议
不多说,直接上干货! 前言 一种最终一致的算法,paxos算法. paxos算法是由大牛lamport发明的,关于paxos算法有很多趣事.比如lamport论文最初由故事描述来引入算法,以至于那班习 ...