NIO Selector works in

2010-07-31  来源:本站原创  分类:Internet  人气:134 

NIO Selector works in

In JDK1.4.2 added support for NIO to master one of the Selector think is the key to achieve good NIO.

Selector is used to obtain the registration of the channel in which the relevant events, that is, accept, read and write. selector has three key set.

key set: which includes representatives of all registered channel, you can selector.keys () are.
selected-key set: contains all be detected are concerned about the operation in place of the key, through selector.selectedKeys be.
cancelled-key set: contains all have been cancel, but no channel have not deregister the key, this set is not directly accessible.

key channel to register by calling the method is added to the key set in the. Canceled when the key in the select will be the key set from the corresponding removed.

key set itself can not be directly modified. Either by calling the close method or call channel key to cancel method, key will be placed in the canceled-key set. The key will cancel the channel will next select registration revoked, at the same time from all the key set key removed.

key in the implementation of the select operation is added to the selected-key set in the. In the selected-key set in the key can call the iterator's remove method, from the selected-key set is removed, can not approach it from the other selected-key set is removed.

The three methods by selector select (block select, until time of the incident out of concern obstruction), selectNow (non-blocking options), select (long) (specify time-out choices, out there are concerned about the arrival time of the incident or exit block ) to get concerned about the incident. The steps are divided into the following three steps:

1, will be present in the canceled-key set in the key from the key set of all removed, a revocation of the channel, clear the canceled-key set.
2, the operating system checks whether the formation events of concern, there is concern when the event occurs, first check the channel's key is already in the selected-key set, if not there, it is added to the selected-key set of to, and modify the key's ready-operation set ready to show the current operation, which previously existed in the ready-operation set of information will be deleted. If the corresponding key already exists in the selected-key set in, which directly modify its ready-operation set to indicate the current operation ready, remove the original ready-operation set of information.
3, if the second step in to canceled-key set in the key, the first step in this step will be the implementation of the operation.

selector itself is thread-safe, but his key set is not. Place in a selection process, the key changes concern the incident until the next select the time to take effect. In addition, key representatives of the channel and its may be cancel at any time and close. There is therefore the key in the key set does not mean that the key is valid, it does not mean the channel is open the. If the key may be canceled or closed the other thread channel, the synchronization program must carefully check these conditions.

The select can be blocked by calling the selector's wakeup method to wake up.

相关文章
  • NIO Selector works in 2010-07-31

    NIO Selector works in In JDK1.4.2 added support for NIO to master one of the Selector think is the key to achieve good NIO. Selector is used to obtain the registration of the channel in which the relevant events, that is, accept, read and write. sele

  • Reposted elsewhere - Java NIO Selector Mechanism for the library (below) 2010-06-14

    http://blog.csdn.net/haoel/archive/2008/03/27/2224069.aspx Selector Mechanism for the Java NIO library (below) Zhao Kun Chen Hao http://blog.csdn.net/haoel <<<<Click here to view the article on the five articles, puzzled: Why should their cons

  • Java NIO Selector 2014-02-04

    A Selector is a Java NIO component which can examine one or more NIO Channel's, and determine which channels are ready for e.g. reading or writing. This way a single thread can manage multiple channels, and thus multiple network connections. Here is

  • nio Selector 阻塞 唤醒 原理 2015-01-03

    Selector:java nio无阻塞io实现的关键. 阻塞io和无阻塞io: 阻塞io是指jdk1.4之前版本面向流的io,服务端需要对每个请求建立一堆线程等待请求,而客户端发送请求后,先咨询服务端是否有线程相应,如果没有则会一直等待或者遭到拒 绝请求,如果有的话,客户端会线程会等待请求结束后才继续执行. 当并发量大,而后端服务或客户端处理数据慢时就会产生产生大量线程处于等待中,即上述的阻塞. 无阻塞io是使用单线程或者只使用少量的多线程,每个连接共用一个线程,当处于等待(没有事件)的时候线

  • [Transfer] Selector.wakeup achieved Notes 2010-11-14

    Copyright Information: Original Address: http://www.blogjava.net/killme2008/archive/2010/10/22/335861.html Original author: dennis.zane NIO Selector package in the underlying system calls, which used to wake up wakeup blocked thread in the select met

  • The development of advanced non-blocking NIO Android package (a) 2010-12-02

    For Android to improve the performance of network communications, we can use on the high-performance Java NIO (New I / O) technology for processing, NIO is introduced in JDK 1.4, NIO N-Noblocking that we can be understood as meaning non-blocking , co

  • The development of advanced non-blocking NIO Android package (c) 2010-12-02

    Android NIO about the essence of the Socket is mainly used for high load network transmission, compared with traditional I / O model of the advantages of Socket transmission, we have advanced in the development of Android NIO non-blocking packet (I)

  • NIO asynchronous communications 2011-10-18

    1.NIO defined NIO is a non-blocking IO, IO compared to traditional terms, to solve two problems: 1) read and write operations are no longer synchronized block; 2) a server at a time can access a client receives, by starting a new thread to improve ac

  • Java NIO 系列教程 2013-06-09

    (一) Java NIO 概述 Java NIO 由以下几个核心部分组成: Channels Buffers Selectors 虽然Java NIO 中除此之外还有很多类和组件,但在我看来,Channel,Buffer 和 Selector 构成了核心的API.其它组件,如Pipe和FileLock,只不过是与三个核心组件共同使用的工具类.因此,在概述中我将集中在这三个组件上.其它组件会在单独的章节中讲到. Channel 和 Buffer 基本上,所有的 IO 在NIO 中都从一个Chann

  • Java NIO学习笔记之一-资料整理 2013-09-06

    最近要用Java写一个socks proxy,本来基于jsocks改了改,但是jsocks的BIO和线程模型到了高并发下简直惨不忍睹.想了一下,最后决定用NIO重写一个socks proxy了,为了方便,可能会忽略掉socks5的认证部分. 说来惭愧,用Java这么久了,对NIO的理解完全停留在IO模型的概念上,一直没有真正写过一个意义上的NIO程序,所以趁机详细学习一下. NIO核心API @左耳朵耗子写过一篇关于NIO Selector的分析http://blog.csdn.net/haoe

  • Java NIO Introduction 2014-02-04

    Java NIO (New IO) is an alternative IO API for Java (from Java 1.4), meaning alternative to the standard Java IO API's. Java NIO offers a different way of working with IO than the standard IO API's. Java NIO: Channels and Buffers In the standard IO A

  • Android development of a number of important points online 2011-01-13

    From network 1. Android Single Instance Methods We all know that Android platform is not the task manager, and defenders within the App Activity history stack to achieve a window display and destruction, for the conventional view is from the shortcut

  • hbase source analysis of the master chapter 1 2011-03-24

    master boot process: -> First initialization HMaster -> Create a rpcServer, and start them -> Listener start a thread, the function is to monitor the client's request, the request into the nio request queue, the logic is as follows: -> Create

  • MINA, xSocket the performance of the same defects and traps, Grizzly better 2009-06-28

    MINA, Grizzly [grizzly-nio-framework], xSocket are based on java nio the server framework. The focus here, defect performance means that when a channel on the SelectionKey.OP_READ ready when, 1. Is a select thread after reading the data distributed t

  • More than one thread is rather deep paper 2010-03-28

    Multi-threaded programming model used on the server: Chen Shuo (solstice01 March 24, 2010) This article about my personal development in a multi-threaded some superficial experience. Summed up the 12 kinds of commonly used thread model, summarizes th

  • Synchronous and asynchronous IO IO difference 2010-07-12

    Synchronous and asynchronous IO IO difference Must first be clear: the difference between different IO model is essentially a CPU, participatory approach Key said here about their own scenarios How to select synchronous or asynchronous it? There are

  • 多线程服务器的常用编程模型 2015-04-10

    本文主要讲我个人在多线程开发方面的一些粗浅经验.总结了一两种常用的线程模型,归纳了进程间通讯与线程同步的最佳实践,以期用简单规范的方式开发多线程程序. 文中的"多线程服务器"是指运行在 Linux 操作系统上的独占式网络应用程序.硬件平台为 Intel x64 系列的多核 CPU,单路或双路 SMP 服务器(每台机器一共拥有四个核或八个核,十几 GB 内存),机器之间用百兆或千兆以太网连接.这大概是目前民用 PC 服务器的主流配置. 本文不涉及 Windows 系统,不涉及人机交互界面

  • 开源软件网站 2010-12-19

    SourceForge.net 以下是编程技术相关网站 http://special.e800.com.cn/080504/ 深入解析Java网络编程 http://www.linuxpk.com/type216_1.html Linux-网络编程 http://violasogni.javaeye.com/blog/548694 Windows环境下APACHE+TOMCAT配置---mod_jk.so http://cwfmaker.javaeye.com/?page=4&show_full

  • MINA,xSocket同样的性能缺陷及陷阱,Grizzly better 2013-01-22

    本文转自博客:http://www.blogjava.net/adapterofcoms/articles/314560.html 因最近想重构下底层的服务器代码,特意查了下开源框架nio的东西 下面的不错 大家一起学校下 :evil: [url][/url] MINA,Grizzly[grizzly-nio-framework],xSocket都是基于 java nio的 server framework. 这里的性能缺陷的焦点是指当一条channel上的SelectionKey.OP_REA

  • Netty 3.1 中文用户手册(三)-架构总览 2013-08-24

    第二章. 架构总览 在这个章节,我们将阐述Netty提供的核心功能以及在此基础之上如何构建一个完备的网络应用. 2.1. 丰富的缓冲实现 Netty使用自建的buffer API,而不是使用NIO的ByteBuffer来代表一个连续的字节序列.与ByteBuffer相比这种方式拥有明显的优势.Netty使用新的buffer类型ChannelBuffer,ChannelBuffer被设计为一个可从底层解决ByteBuffer问题,并可满足日常网络应用开发需要的缓冲类型.这些很酷的特性包括: 如果需