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 anUnsupportedOperationExceptionto 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 ...
随机推荐
- response小结(一)——用response向客户端输出中文数据(乱码问题分析)
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象,和代表响应的response对象.request和response对象既然代表请求和响应,那我们要 ...
- java SimpleDateFormat非线程安全测试
public class MyThread extends Thread { private SimpleDateFormat sdf; private String dateString; publ ...
- css笔记——关于 body/html 的高度百分比
body{ height:100%; 视窗的高度 min-height:100%;文档的具体高度} 这两个百分比的具体高度在页脚永远放在文档底部非常重要,此时用min-height:100% 具体 ...
- IPoint从自定义的投影坐标系转换到自定义的地理坐标系
IPoint pointStart = new PointClass(); pointStart = xyPolyline.FromPoint; ISpatialReferenceFactory pS ...
- What are Upgrade, Product and Package Codes used for? By pusu
Following content is reprinted from here, please go to the original website for more information. Au ...
- Windows Phone 8.1 列表控件(2):分组数据
说到 List 控件,Windows Phone 8.1 上推荐使用的是 ListView 和 GridView. 而这两个控件实在太多东西可讲了,于是分成三篇来讲: (1)基本 (2)分组数据 (3 ...
- python 函数应用
#函数的参数就是个变量 #定义函数的时候,使用关键字参数,可以指定默认值 def hello(name='reboot',age=1): return 'hello %s,your age is %s ...
- [CSS]下拉菜单
原理:先让下拉菜单隐藏,鼠标移到的时候在显示出来 1>display 无动画效果,图片是秒出 2>opacity 有动画效果,我这里是1S出现,推荐配合绝对定位使用
- struts2中的常量
struts2中的常量: 在:struts2-core-2.1.8.1\org\apache\struts2\default.properties 文件里 <!-- 配制i18n国际化--> ...
- ASP.NET 将DataTable解析成JSON简介
这里解析json使用的是Newtonsoft.Json.dll程序集.下面请看code: using System; using System.Collections.Generic; using S ...