NIO的Selector
参考自
- 所有连接注册到一个管理组件,当它们的状态改变(比如有数据可读、可写),就向这个管理组件发出信息。即,这个管理组件被动地监听
- 一个管理组件主动地一直轮询所有组件。
读选择器建立在非阻塞模式上,所以只有通道是非阻塞模式它才会工作。如果你喜欢,还可以让实际的选择处理也变成非阻塞的。重点是Selector对象会帮助你卖力地检查大量通道的状态,你只需要操作选择的结果而不用亲自去管理每一个通道。
Selector 的出现,大大改善了多个 Java Socket的效率。在没有NIO的时候,轮询多个socket是通过read阻塞来完成,即使是非阻塞模式,我们在轮询socket是否就绪的时候依然需要使用系统调用。而Selector的出现,把就绪选择交给了操作系统(我们熟知的selec函数),把就绪判断和读取数据分开,不仅性能上大有改善,而且使得代码上更加清晰。
Java NIO的选择器部分,实际上有三个重要的类。
1,Selector 选择器,完成主要的选择功能。select(), 并保存有注册到他上面的通道集合。 如何通过selector获得所有注册的通channel?
2,SelectableChannel 可被注册到Selector上的通道。 包括啥,不包括啥?
3,SelectionKey 描述一个Selector和SelectableChannel的关系。并保存有通道所关心的操作。 怎么通过SelectionKey获得通道所关心的操作? 什么叫做“通道所关心的操作”?
2,SelectableChannel 可被注册到Selector上的通道。 包括啥,不包括啥? 包括DatagramChannel, Pipe.SinkChannel, Pipe.SourceChannel,ServerSocketChannel, SocketChannel 。不包括FileChannel
3,SelectionKey 描述一个Selector和SelectableChannel的关系。并保存有通道所关心的操作。 怎么通过SelectionKey获得通道所关心的操作? 什么叫做“通道所关心的操作”? SelectionKey的interestOps()返回interest set,这是一个int值。
The interest set determines which operation categories will be tested for readiness the next time one of the selector's selection methods is invoked. The interest set is initialized with the value given when the key is created; it may later be changed via the
interestOps(int)
method.- 即,这个channel会被检测发生了什么事件。比如成功建立连接,可以接受新连接,可读,可写
- SelectionKey.OP_CONNECT 当一个socket channel成功连接到另一个服务器时,称为“连接就续”, 通过isConnectable()判断。
- SelectionKey.OP_ACCEPT 当一个server socket channel准备好接收新进入的连接,称为“连接就绪”。isAcceptable()
- SelectionKey.OP_READ 当一个channel有数据可读时,称为“读就绪”. isReadable()
- SelectionKey.OP_WRITE 当一个channel等待被写入时,称为"写就绪". isWritable()
即interest set用来在select过程中用来测试一个channel己经准备好了进行哪些操作。
public abstract Set<SelectionKey> selectedKeys()
Keys may be removed from, but not directly added to, the selected-key set. Any attempt to add an object to the key set will cause anUnsupportedOperationException
to be thrown.
SelectableChannel
with a Selector
.select方法
select()方法返回的int值表示有多少通道已经就绪。亦即,自上次调用select()方法后有多少通道变成就绪状态。注意,是”之间“有多少通道就绪。所以可能不等于就绪的通道总数。
Selection
During each selection operation, keys may be added to and removed from a selector's selected-key set and may be removed from its key and cancelled-key sets. Selection is performed by the
select()
,select(long)
, andselectNow()
methods, and involves three steps:
Each key in the cancelled-key set is removed from each key set of which it is a member, and its channel is deregistered. This step leaves the cancelled-key set empty.
The underlying operating system is queried for an update as to the readiness of each remaining channel to perform any of the operations identified by its key's interest set as of the moment that the selection operation began. For a channel that is ready for at least one such operation, one of the following two actions is performed:
If the channel's key is not already in the selected-key set then it is added to that set and its ready-operation set is modified to identify exactly those operations for which the channel is now reported to be ready. Any readiness information previously recorded in the ready set is discarded.
Otherwise the channel's key is already in the selected-key set, so its ready-operation set is modified to identify any new operations for which the channel is reported to be ready. Any readiness information previously recorded in the ready set is preserved; in other words, the ready set returned by the underlying system is bitwise-disjoined into the key's current ready set.
If all of the keys in the key set at the start of this step have empty interest sets then neither the selected-key set nor any of the keys' ready-operation sets will be updated.
If any keys were added to the cancelled-key set while step (2) was in progress then they are processed as in step (1).
Whether or not a selection operation blocks to wait for one or more channels to become ready, and if so for how long, is the only essential difference between the three selection methods.
close()
用完Selector后调用其close()方法会关闭该Selector,且使注册到该Selector上的所有SelectionKey实例无效。通道本身并不会关闭。
NIO的Selector的更多相关文章
- Java NIO类库Selector机制解析(下)
五. 迷惑不解 : 为什么要自己消耗资源? 令人不解的是为什么我们的Java的New I/O要设计成这个样子?如果说老的I/O不能多路复用,如下图所示,要开N多的线程去挨个侦听每一个Channel ...
- Java NIO类库Selector机制解析(上)
一. 前言 自从J2SE 1.4版本以来,JDK发布了全新的I/O类库,简称NIO,其不但引入了全新的高效的I/O机制,同时,也引入了多路复用的异步模式.NIO的包中主要包含了这样几种抽象数据类型: ...
- Java NIO 选择器(Selector)的内部实现(poll epoll)
http://blog.csdn.net/hsuxu/article/details/9876983 之前强调这么多关于linux内核的poll及epoll,无非是想让大家先有个认识: Java NI ...
- NIO组件Selector调用实例
*对于nio的非阻塞I/O操作,使用Selector获取哪些I/O准备就绪,注册的SelectionKey集合记录关联的Channel这些信息.SelectionKey记录Channel对buffer ...
- NIO组件Selector工作机制详解(上)
转自:http://blog.csdn.net/haoel/article/details/2224055 一. 前言 自从J2SE 1.4版本以来,JDK发布了全新的I/O类库,简称NIO,其不但 ...
- Java NIO类库Selector机制解析--转
一. 前言 自从J2SE 1.4版本以来,JDK发布了全新的I/O类库,简称NIO,其不但引入了全新的高效的I/O机制,同时,也引入了多路复用的异步模式.NIO的包中主要包含了这样几种抽象数据类型: ...
- Java NIO之Selector(选择器)
历史回顾: Java NIO 概览 Java NIO 之 Buffer(缓冲区) Java NIO 之 Channel(通道) 其他高赞文章: 面试中关于Redis的问题看这篇就够了 一文轻松搞懂re ...
- Nio使用Selector客户端与服务器的通信
使用NIO的一个最大优势就是客户端于服务器自己的不再是阻塞式的,也就意味着服务器无需通过为每个客户端的链接而开启一个线程.而是通过一个叫Selector的轮循器来不断的检测那个Channel有消息处理 ...
- Java NIO 选择器(Selector)的内部实现(poll epoll)(转)
转自:http://blog.csdn.net/hsuxu/article/details/9876983 之前强调这么多关于linux内核的poll及epoll,无非是想让大家先有个认识: Java ...
随机推荐
- error LNK2005: DDX_Control 已经在 uafxcwd.lib(wincore2.obj) 中定义
编译错误提示: 1>afxnmcdd.lib(wincore2.obj) : error LNK2005: "void __stdcall DDX_Control(classCData ...
- Spring使用总结
一.基础JAR包 spring-beans.jar spring-context.jar spring-core.jar spring-expression.jar 二.XML的配置 1.一级结构 & ...
- using System.Reflection;
基础代码: public interface IDBHelper { void Query(); } public class DBHelper : IDBHelper { public int Id ...
- CSS常见的浏览器前缀
为了让浏览器识别某些专属属性,有时候需要在CSS属性前增加浏览器前缀 -ms-:Microsoft IE -moz-:Mozilla Firefox -o-:Opera Opera -webkit-: ...
- ICallbackEventHandler 接口实现回调处理功能
在最近的项目实现中遇到了一个问题 在数据处理的过程中,需要请求获取数据,再做处理之后,可以在页面及时获取数据 开始时,首先想到的到是写Ajax请求,但在做后续数据处理后,处理获取数据等操作,感觉实现起 ...
- JSON-lib框架,JAVA对象与JSON、XML之间的相互转换
Json-lib可以将Java对象转成json格式的字符串,也可以将Java对象转换成xml格式的文档,同样可以将json字符串转换成Java对象或是将xml字符串转换成Java对象. 一. 准备工作 ...
- Jquery get parameter value
http://www.sitepoint.com/url-parameters-jquery/ $.urlParam('id') ==> $.urlParam = function(name){ ...
- Editplus 中将文本换行替换为<p>标签的正则表达式
在Editplus.Notepad++编辑器里文本直接复制到在线编辑器里是不带<p>标签的,只是简单的将换行"\n"替换为"<br />" ...
- Using jQuery to add a dynamic “Back To Top” floating button with smooth scroll
Ever read a really long blog post or article and then had to scroll all the way up to the top of the ...
- iphone开发第二个程序
此程序包括UIProgressView,UIButton, UIDatePicker,UIAlert,UILabel,NSTimer // // HViewController.h // Btn_ ...