初识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 ...
随机推荐
- verilog中读取文件中的字符串_modelsim高级仿真
今天给个程序大家玩玩.因为今天遇到一个问题,就是要向UART发送指令,指令非常多,都是字符串.一直copy 函数 UART ("COMM_1"); UART ("COM ...
- python mysql多条插入
程序的目的是把文本里面的数据存储到数据库中,原来的思路是读一条,插入一条,结果就是时间长的不得了...18万条的数据,真是慢. 后来的想法是把所有的记录都读到一个list里,结果是mysql奔溃go ...
- 再探java基础——break和continue的用法
再探java基础——break和continue的用法 break break可用于循环和switch...case...语句中. 用于switch...case中: 执行完满足case条件的内容内后 ...
- JAVA GUI学习 - 窗口【x】按钮关闭事件触发器:重写processWindowEvent(WindowEvent e)方法
public class WindowListenerKnow extends JFrame { public WindowListenerKnow() { this.setBounds(300, 1 ...
- Runtime.exec使用错误导致延迟.md
这篇文章是纪录了一个bug解决的过程,可是我还是没有可以真正地找出bug的缘由.希望大牛可以详解. 问题的发现 当接触的系统越来越大的时候,对于系统的性能越来越高的时候,找到表面问题的真正原因就慢慢地 ...
- python基础补充
关于模块导入方式: import random print random.choice(range(10)) 和 from random import choice print choice(ra ...
- Linq to sql介绍及增、删、改、查
LinQ是指集成化查询语言,通过映射将数据库内的表名变为C#的类名,将列名作为属性名,将表的关系作为类的成员对象.O--M--R O-Object对象 R-Relation关系 M-Mapping映射 ...
- PHP - 数组去重,(折中:符串去重)
[译]更快的方式实现PHP数组去重 Jan 11, 2016 • Hector 原文:Faster Alternative to PHP’s Array Unique Function 概述 使用PH ...
- [译]在 64bit 环境中执行32 bit的SSIS包
最近碰到了一个众所周知的32bit的 SSIS 包在64bit的SQL Server环境的执行问题. 我看了一些解决方案,比如修改在SSDT里面修改 debug 选项(Run64BitRuntime ...
- Ubuntu基本设置
(1) 为了启用 root 帐号 (也就是 设置一个口令) 使用: sudo passwd root 1.设置IP, 终端输入 sudo gedit /etc/network/interfaces ...