`
Donald_Draper
  • 浏览: 988706 次
社区版块
存档分类
最新评论

Channel接口定义

    博客分类:
  • NIO
阅读更多
NIO-TCP通信实例:http://donald-draper.iteye.com/blog/2369052
    在前面一节中我们写了一通信实例,包括多线程和单线程版本,接下来我们讲从源码来看一下ServerSocketChannel,SocketChannel,Select相关方法,我们先来看一下Channel的定义。之所以看通道接口的定义,主要因为具体的通道实现类的定级父类为Channel:
//ServerSocketChannel
public abstract class ServerSocketChannel
    extends AbstractSelectableChannel
    implements NetworkChannel


//AbstractSelectableChannel
public abstract class AbstractSelectableChannel
    extends SelectableChannel


//SelectableChannel
public abstract class SelectableChannel
    extends AbstractInterruptibleChannel
    implements Channel

来看Channel的定义:
package java.nio.channels;

import java.io.IOException;
import java.io.Closeable;


/**
 * A nexus for I/O operations.
 *
 Channel为IO操作服务的。
 *  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.
 *
 一个通道表示对一个实体的打开连接,比如硬件设备,文件,网络socket,或者一个应用组件
 可能执行一个或多个不同的IO操作,比如读写。
 * <p> A channel is either open or closed.  A channel is open upon creation,
 * and once closed it remains closed.  Once a channel is closed, any attempt to
 * invoke an I/O operation upon it will cause a {@link ClosedChannelException}
 * to be thrown.  Whether or not a channel is open may be tested by invoking
 * its {@link #isOpen isOpen} method.
 *
 通道有两个状态一个打开,一个关闭。通道在创建时打开,一旦关闭将会关闭。如果通道已经关闭,
 尝试执行IO操作,将会引起ClosedChannelException异常。判断一个通道是否打开,可以用isOpen方法。
 * <p> Channels are, in general, intended to be safe for multithreaded access
 * as described in the specifications of the interfaces and classes that extend
 * and implement this interface.
 *
 一般情况下,在实现Channel的具体接口和类中,必须保证多线程安全访问。
 *
 * @author Mark Reinhold
 * @author JSR-51 Expert Group
 * @since 1.4
 */

public interface Channel extends Closeable {

    /**
     * Tells whether or not this channel is open.  

     *
     判断通道是否打开,打开返回true
     * @return <tt>true</tt> if, and only if, this channel is open
     */
    public boolean isOpen();

    /**
     * Closes this channel.
     *
     *  After a channel is closed, any further attempt to invoke I/O
     * operations upon it will cause a {@link ClosedChannelException} to be
     * thrown.
     *如果通道已经关闭, 尝试执行IO操作,将会引起ClosedChannelException异常。
     * <p> If this channel is already closed then invoking this method has no
     * effect.
     如果通道已经关闭,再次调用,则方法不起作用
     * <p> This method may be invoked at any time.  If some other thread has
     * already invoked it, however, then another invocation will block until
     * the first invocation is complete, after which it will return without
     * effect. 

     *
     如果当前线程close时,其他线程已将调用close,则当前线程阻塞,直至先前线程完成close。
     当前线程的close将无效。
     * @throws  IOException  If an I/O error occurs
     */
    public void close() throws IOException;

}

//Closeable
package java.io;

import java.io.IOException;

/**
 * A {@code Closeable} is a source or destination of data that can be closed.
 * The close method is invoked to release resources that the object is
 * holding (such as open files).
 *
 Closeable表示一个数据源或目的可以被关闭,当资源被某个对象持有时(比如打开文件),可以调用
 close关闭资源。
 * @since 1.5
 */

public interface Closeable extends AutoCloseable {

    /**
     * Closes this stream and releases any system resources associated
     * with it. If the stream is already closed then invoking this
     * method has no effect.
     *
     关闭流或释放关联的系统资源。如果流已经关闭,再次调用,则方法不起作用
     * @throws IOException if an I/O error occurs
     */
    public void close() throws IOException;
}


//AutoCloseable
package java.lang;

/**
 * A resource that must be closed when it is no longer needed.
 *
 当一个资源不在需要时,将会关闭,从命名来看自动关闭
 * @author Josh Bloch
 * @since 1.7
 */
public interface AutoCloseable {
    /**
     * Closes this resource, relinquishing any underlying resources.
     * This method is invoked automatically on objects managed by the
     * {@code try}-with-resources statement.
     *
     关闭资源,忽略资源下的所有子资源。资源持有者对象将会在try语句中,自动调用此方法
     * <p>While this interface method is declared to throw {@code
     * Exception}, implementers are [i]strongly[/i] encouraged to
     * declare concrete implementations of the {@code close} method to
     * throw more specific exceptions, or to throw no exception at all
     * if the close operation cannot fail.
     *
     这个接口中声明抛出异常,强烈建议实现close方法,如果关闭资源失败,抛出具体的异常
     * <p>[i]Implementers of this interface are also strongly advised
     * to not have the {@code close} method throw {@link
     * InterruptedException}.[/i]
     *
     接口的实现者,强烈建议不要抛出InterruptedException异常
     * This exception interacts with a thread's interrupted status,
     * and runtime misbehavior is likely to occur if an {@code
     * InterruptedException} is {@linkplain Throwable#addSuppressed
     * suppressed}.
     InterruptedException表示一个线程的中断状态和运行时misbehavior,
     如果一个InterruptedException被Throwable#addSuppressed方法suppressed,
     可能抛出异常。
     * More generally, if it would cause problems for an
     * exception to be suppressed, the {@code AutoCloseable.close}
     * method should not throw it.
     *  
     一般情况下,如果一个异常可以被suppressed,close方法不应该被抛出异常。
     * <p>Note that unlike the {@link java.io.Closeable#close close}
     * method of {@link java.io.Closeable}, this {@code close} method
     * is [i]not[/i] required to be idempotent.  In other words,
     * calling this {@code close} method more than once may have some
     * visible side effect, unlike {@code Closeable.close} which is
     * required to have no effect if called more than once.
     *
     不像Closeable的从close方法,如果调用一次,后面的将会任何影响。而本方法,
     调用多次会有不同的可见效果。
     * However, implementers of this interface are strongly encouraged
     * to make their {@code close} methods idempotent.
     *
     强烈建议实现方法,已经close一次的情况,再次调用无效
     * @throws Exception if this resource cannot be closed
     */
    void close() throws Exception;
}

小节:
一个通道表示对一个实体的打开连接,比如硬件设备,文件,网络socket,或者一个应用组件可能执行一个或多个不同的IO操作,比如读写。通道有两个状态一个打开,一个关闭。通道在创建时打开,一旦关闭将会关闭。如果通道已经关闭,尝试执行IO操作,将会引起ClosedChannelException异常。判断一个通道是否打开,可以用isOpen方法。一般情况下,在实现Channel的具体接口和类中,必须保证多线程安全访问。如果当前线程close时,其他线程已将调用close,则当前线程阻塞,直至先前线程完成close。当前线程的close将无效。
0
0
分享到:
评论

相关推荐

    LVDS屏接口定义

    首先,我们来看一下单6位LVDS屏的接口定义。例如,型号为LP141X3的20针插接口液晶屏。在这个接口中,我们可以看到以下关键信息: 1. VDD:这是电源供应引脚,提供3.3V电压。 2. GND:接地引脚,用于保证电路稳定。 ...

    IDE接口及针脚定义

    IDE接口及针脚定义 IDE(Integrated Drive Electronics)是一种老式的硬盘接口,现在已经逐渐被SATA接口所取代。IDE接口由Compaq和Western Digital公司开发,新版的IDE命名为ATA,即AT bus Attachment。IDE接口在...

    netty权威指南(第二版)

    Channel接口定义了注册、读写、关闭等方法,便于操作网络连接。 - ChannelFuture用于异步操作,可以注册监听器等待操作完成,增强了程序的响应性。 5. **Netty的EventLoop和EventLoopGroup** - EventLoop是Netty...

    netty 权威指南 源代码,第二版

    Channel接口定义了与网络连接相关的读写操作,并且可以监听各种网络事件。 2. **ByteBuf**: Netty的高效字节缓冲区,用于在网络传输中存储和操作数据。相比Java的`ByteBuffer`,`ByteBuf`提供了更方便和高效的API。...

    常见LVDS屏接口定义讲解[定义].pdf

    首先,我们来看一个单6位LVDS屏的接口定义,以LP141X3(20针插接口)为例。在这个接口中,有以下几个关键部分: 1. **电源供应**:VDD表示正3.3V电源,通常有两个引脚提供电源,确保系统稳定工作。同时,GND引脚是...

    硬盘ide 定义说明

    ### 硬盘IDE接口定义详解 在计算机硬件领域中,IDE(Integrated Drive Electronics)是一种早期的硬盘接口标准,用于连接硬盘驱动器到主板。它不仅支持硬盘驱动器,还能支持光驱等其他存储设备。IDE接口通过扁平...

    VGADVIDPHDMI接口及DP面板接口定义[参照].pdf

    本篇将详细解析VGA、DVI、HDMI以及DisplayPort四种常见接口的定义和功能。 首先,VGA(Video Graphics Array)接口是一种模拟接口,由15个针脚组成,主要用于老式显示器。其针脚定义包括: 1. 视频红色:负责红色...

    DVI接口定义及图示

    DVI接口支持多种分辨率和刷新率,可以满足从标准定义到高清晰度电视(HDTV)的各种显示需求。 DVI接口主要有两种类型:DVI-D和DVI-I。DVI-D(Digital Only)仅支持数字信号传输,适用于纯数字显示器,例如LCD或LED...

    inlet_velocity_channel_源项定义_

    标题 "inlet_velocity_channel_源项定义_" 涉及的是在 Fluent 软件中对进气口(inlet)速度通道的源项(source term)进行定义的方法。Fluent 是一个广泛应用的计算流体动力学(CFD)软件,它能够模拟各种流体流动...

    HDMI接口定义及传输流程

    本文将详细阐述HDMI接口的引脚信号定义、传输流程、传输周期、热插拔功能以及Audio Clock等相关知识。 首先,我们来看HDMI接口的引脚配置。HDMI接口主要有A、B、C、D、E五种类型,其中Type A是最常见的。Type A接口...

    SCART接口的引脚定义

    SCART接口,是欧洲的标准视频接口, 传输 CVBS 信号、隔行 RGB 信号,通常厂家都把 SCART 用来传输 RGB 信号。由于三原色信号分开传输,因此在色度方面表现比 S-Video 更好。 SCART 现在只有传输 480I/576I 隔行...

    VN1630(A) DIP-SWITCH Configuration_w.pdf

    VN1630(A)是广州欧科曼科技有限公司推出的一款接口配置设备,主要用于车载通信技术的研发和应用。这款设备支持瑞萨和Vector的相关产品,提供了CAN和LIN接口的灵活性。本文将详细阐述如何通过DIP-SWITCH来配置VN1630...

    GIS与业务系统接口设计[定义].pdf

    在接口设计中,服务端和客户端的关联是通过destination和channel、endpoint实现的。destination定义了客户端要访问的服务,channel指定了通信通道。客户端只需指定destination和回调函数,其余通信细节由BlazeDS自动...

    HDMI接口定义

    当HPD变为高电平时,源设备会开始接收接收端设备的E-EDID(Enhanced Extended Display Identification Data)信息,这是关于显示设备能力的详细数据,通过DDC(Display Data Channel)通道传输,使得源设备能自动...

    Fibre Channel for SANs

    FC协议定义了五种不同的服务等级,分别是:Class 1、Class 2、Class 3、Class F和Class W。这些服务等级主要区别在于错误处理和确认机制,其中Class 1和Class 2提供了错误检测和恢复功能,而Class 3则不提供任何确认...

    typec耳机原理描述

    总结而言,Type-C耳机的实现原理和关键知识点包括了硬件接口定义、状态转换机制、音频信号的模拟与数字传输方式、芯片设计要求,以及电力传输和兼容性问题的解决。随着技术的不断演进,Type-C耳机在未来的消费电子...

    互联网电视接口规范

    每个元素都对应了特定的业务逻辑和管理功能,例如,PROGRAM定义了节目的具体信息,CAST定义了演员信息,CHANNEL定义了频道的属性等。 规范的最后部分还包含了一些附录,提供了视音频参数的定义、XML Schema(XML...

    ppp_channel.rar_between

    接口定义是编程中的关键部分,它确保了PPP协议能够正确地通过通道发送和接收数据,并处理各种控制信息,如链路建立、认证、错误检测和纠正。 文件“ar7part.c”可能是PPP实现的一部分,但具体功能无法仅凭文件名...

    PWM 11channel.zip

    源代码中可能会定义了函数或类,用于初始化PWM控制器、设置PWM通道、配置PWM频率和占空比等功能。例如,可能有如下函数: 1. `init_PWM()`: 初始化整个PWM模块,包括配置时钟分频器、选择PWM模式等。 2. `set_...

    光纤通道(FC-Fibre+Channel)+.doc

    光纤通道层次模型分为五个层次:FC-0到FC-4,定义了物理层的特征、传输控制方法、与TCP/IP、SCSI-3、HiPPI等协议的上层接口。 一、光纤通道层次模型 光纤通道是一种基于标准的网络结构,定义了物理层的特征、传输...

Global site tag (gtag.js) - Google Analytics