初识Channel
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的更多相关文章
- GO语言之channel
前言: 初识go语言不到半年,我是一次偶然的机会认识了golang这门语言,看到他简洁的语法风格和强大的语言特性,瞬间有了学习他的兴趣.我是很看好go这样的语言的,一方面因为他有谷歌主推,另一方面他确 ...
- Redis——学习之路三(初识redis config配置)
我们先看看config 默认情况下系统是怎么配置的.在命令行中输入 config get *(如图) 默认情况下有61配置信息,每一个命令占两行,第一行为配置名称信息,第二行为配置的具体信息. ...
- 《Go并发编程实战》读书笔记-初识Go语言
<Go并发编程实战>读书笔记-初识Go语言 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在讲解怎样用Go语言之前,我们先介绍Go语言的特性,基础概念和标准命令. 一. ...
- 联盟链初识以及Fabric环境搭建流程
这篇文章首先简单介绍了联盟链是什么,再详细的介绍了Fabric环境搭建的整个流程. 区块链分类: 以参与方式分类,区块链可以分为:公有链.联盟链和私有链. 定义: 我们知道区块链就是一个分布式的,去中 ...
- 初识zookeeper(1)之zookeeper的安装及配置
初识zookeeper(一)之zookeeper的安装及配置 1.简要介绍 zookeeper是一个分布式的应用程序协调服务,是Hadoop和Hbase的重要组件,是一个树型的目录服务,支持变更推送. ...
- Android-Window(一)——初识Window
Android-Window(一)--初识Window 学习自 <Android开发艺术探索> https://blog.csdn.net/qian520ao/article/detail ...
- Go从入门到精通(一)go语言初识
一.第一个go程序 package main import ( "fmt" ) func main(){ fmt.Println("hello world") ...
- Java NIO学习与记录(一):初识NIO
初识 工作中有些地方用到了netty,netty是一个NIO框架,对于NIO却不是那么熟悉,这个系列的文章是我在学习NIO时的一个记录,也期待自己可以更好的掌握NIO. 一.NIO是什么? 非阻塞式I ...
- 初识Golang编程语言
初识Golang编程语言 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Go 是年轻而有活力的语言,有网友说:"Go语言将超过C,Java,成为未来十年最流行的语言&qu ...
随机推荐
- cocoapod的安装与使用
cocoaPods的使用 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ...
- 转: 深入理解 AngularJS 的 Scope
查看 DEMO.参考 StackOverflow. ng-switch ng-switch 的原型继承和 ng-include 一样.所以如果你需要对基本类型数据进行双向绑定,使用 $parent ...
- tcpdump抓包并保存成cap文件
首选介绍一下tcpdump的常用参数 tcpdump采用命令行方式,它的命令格式为: tcpdump [ -adeflnNOpqStvx ] [ -c 数量 ] [ -F 文件名 ] [ -i 网络接 ...
- HDU 1290 献给杭电五十周年校庆的礼物
题解:http://www.cnblogs.com/forever97/p/3522238.html #include <cstdio> int main() { int n; while ...
- JAVA GUI学习 - 窗口【x】按钮关闭事件触发器:重写processWindowEvent(WindowEvent e)方法
public class WindowListenerKnow extends JFrame { public WindowListenerKnow() { this.setBounds(300, 1 ...
- struts2自己定义类型转换器
1.1. struts2自己定义类型转换器 1) 自定类型转换类,继承DefaultTypeConverter类 package com.morris.ticket.conversio ...
- android程序报错“error launching activity com.android.ddmlib.shellcommandunresponsiveexception”的解决方式
今天在调试android程序的时候,因为是NDK开发,要先编译.so库再打包下载到android模拟器,所以花费的时间比較长.控制台就会报例如以下错误: error launching activit ...
- HTML5+CSS3正式开课
HTML5+CSS3 HTML5+CSS3 HTML5+CSS3
- js 解析XML 在Edge浏览器下面 无法准确读到节点属性值
js 解析XML 在Edge浏览器下面 无法准确读到节点属性值 Dom.documentElement.childNodes[j].attributes[2] 这个是大众写法 在win10的edge ...
- 重启IIS报错:IIS 服务或万维网发布服务,或者依赖这 服务可能在启动期间发生错误或者已禁用
参考文章: http://www.cnblogs.com/zengen/archive/2010/10/29/1864569.html 开启如下服务: Net.Msmq Listener Adapte ...