JAVA NIO 翻译系列(六、Selector)

selector是一个检测一个或者多个channel的组件,能侦测哪个channel装备好写或者读操作。这种方式下,一个单线程就可以管理多个channel或者多个网络链接。


为什么使用selector?

使用单线程去处理多channel的优势就是你只需要少量的线程去处理多channel。甚至,你可以用一个线程去处理所有的channel。对操作系统来说,线程切换是很费资源的,并且在操作系统中,每个线程都会占据资源(内存),因此线程越少,越好。

但请记住,现代的操作系统和cpu在多任务处理上正变的越来越好,多线程花费的时间越来越少。实际上,如果一个cpu是多核的,你不做多任务处理的话,可能正是一种资源浪费。总之,这块属于不同区域的讨论。仅对此处而言,一个selector可以用一个县城来处理多个Channel。


Java NIO: A Thread uses a Selector to handle 3 Channel's

Creating a Selector

创建一个selector:

Selector selector = Selector.open();


注册channel到Selector上:


使用selector的前提就是channel必须是在非阻塞的模式下。这就意味着你不能将FileChannel注册到selector上,前面文章的例子都是FileChannel。但是Socket 相关的channel就能很好的使用selector。

注意register方法的第二个参数,

这是一个有趣的设置,意味着这个channel通过selector对什么事件感兴趣,有以下四个事件可以监听到:

     Connect     Accept     Read    Write

一个Channel启动一个事件代表它为这个事件转备好了。因此,channel链接上服务器就是一个连接准备,一个socket Channel接受一个连接代表接受事件,一个channel有数据准备去读代表一个准备读事件。

如果一个selector对多个事件感兴趣,则

int interestSet = SelectionKey.OP_READ | SelectionKey.OP_WRITE;

传入到register方法中。


SelectionKey

register()方法的返回对象就是SelectionKey。SelectionKey含有几个有意思的特性;



Interest Set

通过以下方法可以获得register()方法的时候的事件类型

你可以使用&连接符连接SelectionKey的常量来找出Selector对哪些事件感兴趣

Ready Set

ready set 是channel的一些列可操作的状态集合,你可以获取这个状态集合通过一个selection。

int readySet = selectionKey.readyOps();

下面的方法跟上面的效果一样

selectionKey.isAcceptable();

selectionKey.isConnectable();

selectionKey.isReadable();

selectionKey.isWritable();


Channel + Selector

你可以通过SelectionKey来访问Channel和Selector对象

Channel  channel  = selectionKey.channel();

Selector selector = selectionKey.selector();


Attaching Objects

你可以在selectionKey附加一个Object对象,来标识channel对象以便找出你要的channel对象,或者附加一些其他的信息。举个栗子,你可以将buffer对象附加在上面

selectionKey.attach(theObject);

Object attachedObj = selectionKey.attachment();

你可以在注册Selector的时候把对象附加上去

SelectionKey key = channel.register(selector, SelectionKey.OP_READ, theObject);


Selecting Channels via a Selector

一旦你注册了一个或者多个channel到一个Selector上面,你就可以调用以下三个select方法。这些方法会返回准备好的事件的对象,这些事件(connect,accept,read 或者write)都是你当初注册Selector时候传递的。换句话说如果你注册的时候是传递的读事件,那么当你调用select的时候,获得当前准备去读的channels。

int select()

int select(long timeout)

int selectNow()

select()会阻塞,直到至少一个已经转备好事件的channel。

select(long timeout) 跟上面方法一样,除了他会在timeout毫秒内会阻塞.

selectNow()不会阻塞,会立即返回


selectedKeys()

当你调用select()方法的时候,返回的值代表有多少个channel准备好了,你可以通过selected key set去访问准备好的channel

Set selectedKeys = selector.selectedKeys();

When you register a channel with aSelectortheChannel.register()method returns aSelectionKeyobject. This key represents that channels registration with that selector. It is these keys you can access via theselectedKeySet()method. From theSelectionKey.

当你调用channel的register的方法的时候,会返回一个SelectionKey。这个代表channel和selector的关系

remove方法是必须要调用的,当你处理好channel。通过SelectionKey.channel()会获得channel对象,你可以类型转换你要的Channel对象,比如ServerSocketChannel等


wakeUp()

一个线程调用select()就会阻塞,但是也可以脱离select()方法回到非阻塞状态,即使没有channel转备好。这种方式就是让其他线程调用那个selector的wakeup()方法,然后select()方法会立即返回。

如果另外一个线程调用这个selector的wakeup方法,并且原来调用selector的select()方法的线程并没有阻塞,那么下一个调用selector的select()方法会立即返回。

close()

与注册相反,这个方法会使所有的selectKey无效

完整的栗子


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容