java.nio.channels 中的接口和类。

 A channel represents an open connection to an entity such as a hardware device, a file, a network socket, or a program component that is capable of performing one or more distinct I/O operations, for example reading or writing.
  channel就是一个打开的连接向一个实体的连接,这些实体包括硬件设备,文件,网络socket或者程序组件,一个能够执行一个或多个I/O操作。
Channel接口:
  channel只有两个状态open close
  所以在这个接口中只有两个方法
      public boolean isOpen();
   public boolean isClose();
ReadableByteChannel 接口:
  实现了Channel接口
  还有     public int read(ByteBuffer dst) throws IOException; 返回从ByteBuffer中读取的字节数量。
WritableByteChannel接口:
  public int write(ByteBuffer src) throws IOException; 写入src中,返回写入的字节数量。
ScatteringByteChannel 接口
  继承了 ReadableByteChannel 并提供了同时往几个 ByteBuffer 中写数据的能力。
     public long read(ByteBuffer[] dsts, int offset, int length)  throws IOException;
      public long read(ByteBuffer[] dsts) throws IOException;
 GatheringByteChannel 接口
  继承了 WritableByteChannel 并提供了同时从几个 ByteBuffer 中读数据的能力。
    public long write(ByteBuffer[] srcs, int offset, int length)       throws IOException
  public long write(ByteBuffer[] srcs) throws IOException;
InterruptibleChannel 
  A channel that can be asynchronously closed and interrupted.
  用来表现一个可以被异步关闭的 Channel 。这表现在两方面:
1.    当一个 InterruptibleChannel 的 close() 方法被调用时,其它 block 在这个 InterruptibleChannel 的 IO 操作上的线程会接收到一个                  AsynchronousCloseException 。
2.    当一个线程 block 在 InterruptibleChannel 的 IO 操作上时,另一个线程调用该线程的 interrupt() 方法会导致 channel 被关闭,该线程收到一个 ClosedByInterruptException ,同时线程的 interrupt 状态会被设置。
  
  介绍完了其中的接口,接下来介绍一下其中的类:


  
  NIO--非阻塞 IO 允许应用程序同时监控多个 channel 以提高性能,这一功能是通过 Selector , SelectableChannel 和 SelectionKey 这 3 个类来实现的。

  他们之间的关系是这样的(个人理解,欢迎拍砖) ---只是画出了其中的一部分 ,从其他数据源来获取数据。

  
SelectionKey
   A selection key is created each time a channel is registered with a selector.  A key remains valid until it is canceled by call cancel method. 当使用selector 注册一个channel的时候就会创建一个SelectionKey。
  Cancelling a key does not immediately remove it from its selector,It is instead added to the selector`s cancelled-key set.

  在SelectionKey 中维护者两个keySet interest set  和 ready set

     The <i>interest set</i> 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.

    The <i>ready set</i> identifies the operation categories for which  the key's channel has been detected to be ready by the key's       selector.  The ready set is initialized to zero when the key is created; it may later  be updated by the selector during a         selection operation, but it cannot be updated directly.

    Selection keys are safe for use by multiple concurrent threads.  Selection keys是线程安全的,其中的读写操作都使用同步方法      加以控制。 

  构造方法;  他不能通过传统的构造函数的方式来获取构造实例。通常使用Selector中的方法来获取所有的可用的SelectionKey 集合,然后再在其中根据他们各自的状态选择自己真正需要的SelectionKey.

     public abstract Set<SelectionKey> selectedKeys(); 或者     public abstract Set<SelectionKey> keys();

  这其中有几个非常重要的判断方法

        public abstract boolean isValid(); // Tells whether or not this key is valid. 判断是否有效

      public final boolean isReadable();//Tests whether this key's channel is ready for reading. 是否可以读取

     public final boolean isWritable() //   Tests whether this key's channel is ready for writing.

  还有几个重要的常量,经常用来对SelectionKey的筛选,选择自己所需。

    OP_READ,OP_WRITE,OP_CONNECT,OP_ACCEPT。后面两个经常用来SocketChannel 和ServerSocketChannel

  另外还可以从中获取selector 和channel

Selector  -- 称之为选择器

  A selector may be created by invoking the  open method of this class.e.g. selector = Selector.open();

  如果想要从channel中获取数据,首先要做的就是将channel 注册到selector上面。

常用方法:

      public abstract boolean isOpen();  Tells whether or not this selector is open

    public abstract int select(long timeout);Selects a set of keys whose corresponding channels are ready for I/O operations;返回当前可用的selecionKeys数量,timeout 表示可以等待的时长。

   public abstract int select() throws IOException; 同上,无需等待直接返回。

SelectableChannel

这个抽象类是所有支持非阻塞 IO 操作的 channel (如 DatagramChannel 、 SocketChannel )的父类。 SelectableChannel 可以注册到一个或多个 Selector 上以进行非阻塞 IO 操作。

SelectableChannel 可以是 blocking 和 non-blocking 模式(所有 channel 创建的时候都是 blocking 模式),只有 non-blocking 的 SelectableChannel 才可以参与非阻塞 IO 操作。

  SelectableChannel configureBlocking(boolean block)    设置 blocking 模式。false,为非阻塞。

boolean isBlocking()      返回 blocking 模式。

通过 register() 方法, SelectableChannel 可以注册到 Selector 上。

  接下来会介绍SelectableChannel的一些子类,欢迎拍砖。

初识Channel的更多相关文章

  1. GO语言之channel

    前言: 初识go语言不到半年,我是一次偶然的机会认识了golang这门语言,看到他简洁的语法风格和强大的语言特性,瞬间有了学习他的兴趣.我是很看好go这样的语言的,一方面因为他有谷歌主推,另一方面他确 ...

  2. Redis——学习之路三(初识redis config配置)

    我们先看看config 默认情况下系统是怎么配置的.在命令行中输入 config get *(如图) 默认情况下有61配置信息,每一个命令占两行,第一行为配置名称信息,第二行为配置的具体信息.     ...

  3. 《Go并发编程实战》读书笔记-初识Go语言

    <Go并发编程实战>读书笔记-初识Go语言 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在讲解怎样用Go语言之前,我们先介绍Go语言的特性,基础概念和标准命令. 一. ...

  4. 联盟链初识以及Fabric环境搭建流程

    这篇文章首先简单介绍了联盟链是什么,再详细的介绍了Fabric环境搭建的整个流程. 区块链分类: 以参与方式分类,区块链可以分为:公有链.联盟链和私有链. 定义: 我们知道区块链就是一个分布式的,去中 ...

  5. 初识zookeeper(1)之zookeeper的安装及配置

    初识zookeeper(一)之zookeeper的安装及配置 1.简要介绍 zookeeper是一个分布式的应用程序协调服务,是Hadoop和Hbase的重要组件,是一个树型的目录服务,支持变更推送. ...

  6. Android-Window(一)——初识Window

    Android-Window(一)--初识Window 学习自 <Android开发艺术探索> https://blog.csdn.net/qian520ao/article/detail ...

  7. Go从入门到精通(一)go语言初识

    一.第一个go程序 package main import ( "fmt" ) func main(){ fmt.Println("hello world") ...

  8. Java NIO学习与记录(一):初识NIO

    初识 工作中有些地方用到了netty,netty是一个NIO框架,对于NIO却不是那么熟悉,这个系列的文章是我在学习NIO时的一个记录,也期待自己可以更好的掌握NIO. 一.NIO是什么? 非阻塞式I ...

  9. 初识Golang编程语言

    初识Golang编程语言 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Go 是年轻而有活力的语言,有网友说:"Go语言将超过C,Java,成为未来十年最流行的语言&qu ...

随机推荐

  1. 4.PHP 教程_PHP 变量

    PHP 变量 变量是用于存储信息的"容器": <?php $x = 5; $y = 6; $z = $x + $y; echo $z; ?> 与代数相似 x=5 y=6 ...

  2. 通过读取excel数据和mysql数据库数据做对比(二)-代码编写测试

    通过上一步,环境已搭建好了. 下面开始实战, 首先,编写链接mysql的函数conn_sql.py import pymysql def sql_conn(u,pwd,h,db): conn=pymy ...

  3. widget intent重复问题

    今天在做android widget时发现点击任意widget时只会更新最后一个widget 原来是requestCode的问题 Intent intent = new Intent(WidgetPr ...

  4. MVC-03 控制器(3)

    Controller负责处理浏览器来的所有要求,并决定响应什么属性给浏览器,以及协调Model与View之间的数据传递.在ASP.NET MVC中有好几种传递数据给视图的方式,例如从ASP.NET M ...

  5. 怪兽z主机豪华版 答问。

    我的淘宝店里,怪兽z主机标准版,分经济版本,标准版,豪华版,三个版本.这里给大家详细介绍一下豪华版的概况. 淘宝购买地址:http://item.taobao.com/item.htm?id=3818 ...

  6. 1TB到底能存放多少东西?

    网盘大战逐渐升级,360和百度网盘先后推出的1TB网盘存储,而腾讯甚至为其微云网盘打出10TB的招牌来哄抢用户. 这里我们聊聊1TB的网盘究竟能放多少东西? 以下是我在网上找到的一些资料. 一)30年 ...

  7. 射频识别技术漫谈(24)——ISO15693的防冲突与传输协议

    遵守ISO15693协议的电子标签都有一个8字节共64bit的全球唯一序列号(UID),这个UID一方面可以使全球范围内的标签互相区别,更重要的是可以在多标签同时读写时用于防冲突.8字节UID按权重从 ...

  8. 射频识别技术漫谈(9)——动物标签HDX

    半双工(HDX,Half Duplex)技术是ISO11784/11785中规定的另一种标签与读写器之间的通讯方式.读写器先打开射频场对标签充电以激活标签,然后关闭磁场,标签在读写器磁场关闭的情况下向 ...

  9. qt 国际化(翻译时会触发changeEvent)

    1. 修改工程文件 .pro ,加入翻译源文件 hello_world.ts: TRANSLATIONS += \        Resource/translations/hello_world.t ...

  10. Windows Azure 网站上的 WordPress 3.8

     编辑人员注释:本文章由 Windows Azure 网站团队的项目经理 Sunitha Muthukrishna 和 Windows Azure 网站开发人员体验合作伙伴共同撰写. WordPr ...