1. 什么是Channel?

channel表示的是在一个实体上打开的连接

实体:

a hardware device

a file

a network socket

a program component that is capable of performing one or more distinct I/O operations, for example reading or writing

2. Channel有哪些状态?

Channel只有open和close两种状态,二者的转换状态如下:

$$

\text{open} \longrightarrow \text{close} \

\text{close} \not\longrightarrow \text{open}

$$

有API可以查看一个channel是不是open状态: isOpen

3. Channel的几个直接接口

3.1 WritableByteChannel

可以往其中写入bytes的channel,几乎总是在一个时间只有一个写操作作用于其上,是否能并发写还得看具体实现

public int write(ByteBuffer src) throws IOException;

该方法的调用时产生很多异常类型可以看出使用该方法的一些注意事项:

method

desc

NonWritableChannelException

If this channel was not opened for writing

ClosedChannelException

If this channel is closed

AsynchronousCloseException

If another thread closes this channel while the write operation is in progress

ClosedByInterruptException

If another thread interrupts the current thread while the write operation is in progress, thereby closing the channel and setting the current thread's interrupt status

3.2 ReadableByteChannel

与WritableByteChannel相反,可以从该channel中读取数据到buffer

public int read(ByteBuffer dst) throws IOException;

读取数据到buffer中,并不一定会填满整个buffer,具体还是得看实现, 例如:

处于non-block模式的socket channel,只会读取socket输入buffer中的数据,不会多读

从一个file channel中最多只能读取到文件中剩余的数据

block模式下,会读取channel中所有的数据直到最后一个byte

3.3 AsynchronousChannel

支持异步IO操作的channel

异步IO操作通常可以分为以下2种形式:

Future operation(...)

void operation(... A attachment, CompletionHandler handler)

param

desc

operation

read/write

V

IO操作的结果类型

A

附加于IO操作之上的对象类型,提供一个``context`

例如:

// AsynchronousFileChannel

// read操作的结果是读到的byte的个数,即V为Integer

public abstract void read(ByteBuffer dst,

long position,

A attachment,

CompletionHandler handler);

public abstract Future read(ByteBuffer dst, long position);

public abstract void write(ByteBuffer src,

long position,

A attachment,

CompletionHandler handler);

public abstract Future write(ByteBuffer src, long position);

3.4 NetworkChannel

socket channel

NetworkChannel bind(SocketAddress local) throws IOException;

NetworkChannel setOption(SocketOption name, T value) throws IOException;

3.5 InterruptibleChannel

可以异步关闭和中断的channel

channel java_Java Channel相关推荐

  1. Channel shutdown: channel error; protocol method

    Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=406, reply ...

  2. Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=406, reply-text=

    完整错误 CachingConnectionFactory.java:1278 - Channel shutdown: channel error; protocol method: #method& ...

  3. channel java_Java中channel用法总结

    本文实例总结了Java中channel用法.分享给大家供大家参考.具体分析如下: 1.Channel接口的定义: public interface Channel { public boolean i ...

  4. Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=

    rabbitTemplate.convertAndSend()这个方法的传参传错了,你检查一下就能解决问题了 举个例子把: 我这里使用的使用的是路由模式 第一个参数:你使用的交换机的名称,MQCons ...

  5. matlab awgn channel,AWGN Channel

    Relationship Between EsN0 and SNR The relationship between EsN0 and SNR, both expressed in dB, is as ...

  6. Netty Channel源码分析

    原文:https://wangwei.one/posts/netty-channel-source-analyse.html 前面,我们大致了解了Netty中的几个核心组件.今天我们就来先来介绍Net ...

  7. 交换环境下的冗余及负载分担技术(STP Channel HSRP)

    首先来说,冗余与负载分担是两个概念.冗余往往与备份是联系在一起的,单纯有冗余技术并不一定能实现负载分担.而负载分担技术是依赖于冗余的. 概括地来讲,交换环境下的三种技术都有冗余及负载分担功能:STP. ...

  8. Go语言 channel

    介绍 Channel是Go中的一个核心类型,你可以把它看成一个管道,通过它并发核心单元就可以发送或者接收数据进行通讯(communication). 要想理解 channel 要先知道 CSP 模型. ...

  9. golang channel 管道 通道 信道 使用总结

    不同于传统的多线程并发模型使用共享内存来实现线程间通信的方式,golang 的哲学是通过 channel 进行协程(goroutine)之间的通信来实现数据共享: > Do not commun ...

  10. golang channel 管道

    channel是Go中的一个核心类型,你可以把它看成一个管道,通过它并发核心单元就可以发送或者接收数据进行通讯(communication). 它的操作符是箭头 <- . ch <- v ...

最新文章

  1. 虚拟dom_通过编写简易虚拟DOM,来学习虚拟DOM 的原理
  2. 类似百度输入框自动完成
  3. Docker的启动与停止命令
  4. echarts统计x轴区间的数值
  5. 这年头,好文案都被它承包了!
  6. mysql多表查询插入更新_Mysql多表查询,多表插入和多表更新
  7. 高效快速中值滤波算法c语言,快速中值滤波及c语言实现.docx
  8. Magento批量生成优惠券
  9. socket 套接字
  10. mybatis动态sql传ist集合參与传数组参数
  11. html 星空效果,html5 canvas炫酷旋转银河系星空背景特效
  12. eclipse各版本对应JDK版本要求
  13. keil中下载程序的擦除功能
  14. 竞品分析:得物APP、nice APP、识货APP
  15. 世界各个国家或地区国际域名缩写(Countries and Regions)
  16. 《三联生活周刊》2017年第26期:3星。终南山隐士现状。
  17. WordPress 安全漏洞
  18. 三维数据可视化软件html5,基于 HTML5 的 WebGL 自定义 3D 摄像头监控模型 | 3D组态|图扑软件|数据可视化|blog...
  19. MySQL WEEK函数介绍
  20. android 学生信息管理系统,android学生信息管理系统的报告.docx

热门文章

  1. python语音库_python音频库dejavu原理浅析
  2. 浅谈CMMI3认证从评估前准备到正式评估的全部过程
  3. 虾米音乐关停:网友泪崩 一代人的青春落幕
  4. 购物网站(测试+步骤+代码)
  5. 写SQL的21个好习惯
  6. the little schemer 笔记(10.1)
  7. 解决win10安装失败原因和方法
  8. 打豆豆游戏c语言编程,《C语言及程序设计》实践参考——打豆豆
  9. 《南怀瑾讲述99个人生道理》——刘清海编著
  10. 【牛客刷题-SQL进阶挑战】NO5.窗口函数