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

AbstractInterruptibleChannel接口定义

    博客分类:
  • NIO
阅读更多
Channel接口定义:http://donald-draper.iteye.com/blog/2369111
前面看了一下通道接口的定义,只要是为了从根本上理解通道,今天来看一下可异步关闭
和中断阻塞IO操作线程的通道接口定义AbstractInterruptibleChannel,下面是ServerSocketChannel实现的类和接口结构树:
//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

从继承和实现树,我们可以看到可选择通道,实际上继承了AbstractInterruptibleChannel,
这就是写这篇文章的原因,下面从源码及Java DOC来分析:

package java.nio.channels.spi;

import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.nio.channels.*;
import java.security.AccessController;
import java.security.PrivilegedAction;
import sun.nio.ch.Interruptible;
/**
 * Base implementation class for interruptible channels.
 *
 AbstractInterruptibleChannel是可中断通道实现类的基础。
 *  This class encapsulates the low-level machinery required to implement
 * the asynchronous closing and interruption of channels.  A concrete channel
 * class must invoke the {@link #begin begin} and {@link #end end} methods
 * before and after, respectively, invoking an I/O operation that might block
 * indefinitely.  In order to ensure that the {@link #end end} method is always
 * invoked, these methods should be used within a
 * <tt>try</tt> ... <tt>finally</tt> block: <a name="be">
 *
AbstractInterruptibleChannel封装了异步关闭和中断通道的底层操作。具体的通道类,
相对于调用不确定的阻塞IO时,必须调用#begin和#end,即执行通道的IO操作之前调用begin方法
,在执行完后调用end方法。为了保证end的总是被执行,方法应为放在一个try语句块的finally中。
下面是一个简单的实例:
 * <blockquote><pre>
 * boolean completed = false;
 * try {
 *     begin();
 *     completed = ...;    // Perform blocking I/O operation
 *     return ...;         // Return result
 * } finally {
 *     end(completed);
 * }</pre></blockquote>
 *
 * <p> The <tt>completed</tt> argument to the {@link #end end} method tells
 * whether or not the I/O operation actually completed, that is, whether it had
 * any effect that would be visible to the invoker.  In the case of an
 * operation that reads bytes, for example, this argument should be
 * <tt>true</tt> if, and only if, some bytes were actually transferred into the
 * invoker's target buffer.
 *
 end方法中的boolean参数completed,用于表示一个IO操作实际上是否完成,即对调用者可见。
 在一个读操作中,当且仅当读操作从通道读取数据到调用者的缓存区中时,参数为true,即
 channel.read(buf),从通道读取数据到缓存区。
 * <p> A concrete channel class must also implement the {@link
 * #implCloseChannel implCloseChannel} method in such a way that if it is
 * invoked while another thread is blocked in a native I/O operation upon the
 * channel then that operation will immediately return, either by throwing an
 * exception or by returning normally.  If a thread is interrupted or the
 * channel upon which it is blocked is asynchronously closed then the channel's
 * {@link #end end} method will throw the appropriate exception.
 *
 一个具体的通道类必须实现#implCloseChannel方法,如果其他线程因本地IO的操作阻塞在通道中,
 此方法调用时,相应的IO操作立刻返回或,抛出异常,或正常返回。如果线程中断或阻塞在通道中,
 异步关闭通道,end方法将会抛出一个相关的异常。
 * <p> This class performs the synchronization required to implement the {@link
 * java.nio.channels.Channel} specification.  Implementations of the {@link
 * #implCloseChannel implCloseChannel} method need not synchronize against
 * other threads that might be attempting to close the channel.  

 *
 如果一个具体的通道为同步的需要实现Channel接口。implCloseChannel方法的实现不需要
 对其尝试关闭通道的线程进行同步。
 *
 * @author Mark Reinhold
 * @author JSR-51 Expert Group
 * @since 1.4
 */

public abstract class AbstractInterruptibleChannel
    implements Channel, InterruptibleChannel
{

    private final Object closeLock = new Object();//关闭锁
    private volatile boolean open = true;

    /**
     * Initializes a new instance of this class.
     */
    protected AbstractInterruptibleChannel() { }

    /**
     * Closes this channel.
     *
     *  If the channel has already been closed then this method returns
     * immediately.  Otherwise it marks the channel as closed and then invokes
     * the {@link #implCloseChannel implCloseChannel} method in order to
     * complete the close operation.  

     *
     如果一个通道已经被关闭,方法将会立即关闭。否则,将标记通道已经关闭,
     然后调用#implCloseChannel方法完成关闭操。
     * @throws  IOException
     *          If an I/O error occurs
     */
    public final void close() throws IOException {
        synchronized (closeLock) {
            if (!open)
                return;
            open = false;
            implCloseChannel();
        }
    }

    /**
     * Closes this channel.
     *
     *  This method is invoked by the {@link #close close} method in order
     * to perform the actual work of closing the channel.  This method is only
     * invoked if the channel has not yet been closed, and it is never invoked
     * more than once.
     *
     当close方法调用时,将调用implCloseChannel完成实际的关闭通道工作。此方仅在通道还没
     完全关闭时调用,此方只会被调用一次。
     * <p> An implementation of this method must arrange for any other thread
     * that is blocked in an I/O operation upon this channel to return
     * immediately, either by throwing an exception or by returning normally.
     * 

     *
     此方的实现,必须通知其他阻塞在通道IO操作的线程,立即返回,或抛出异常,或正常返回。
     * @throws  IOException
     *          If an I/O error occurs while closing the channel
     */
    protected abstract void implCloseChannel() throws IOException;

    public final boolean isOpen() {
        return open;
    }


    // -- Interruption machinery --

    private Interruptible interruptor;//中断处理器
    private volatile Thread interrupted;//中断IO阻塞操作的线程

    /**
     * Marks the beginning of an I/O operation that might block indefinitely.
     *
     标记一个可能会阻塞的IO操作的开始
     *  This method should be invoked in tandem with the {@link #end end}
     * method, using a <tt>try</tt> ... <tt>finally</tt> block as
     * shown [url=#be]above[/url], in order to implement asynchronous
     * closing and interruption for this channel.  

     begin方法应该与end放配合使用,我们一般用一个try语句块中,上面已经给出实例,
     主要是为了实现异步的通道关闭和线程中断。
     */
    protected final void begin() {
        //如果中断处理器为null,创建一个中断处理器,并在中断线程时,记录记录线程
        if (interruptor == null) {
            interruptor = new Interruptible() {
                    public void interrupt(Thread target) {
                        synchronized (closeLock) {
                            if (!open)
                                return;
                            open = false;
			    //记录中断IO操作阻塞线程的线程
                            interrupted = target;
                            try { 
			        //关闭实际通道
                                AbstractInterruptibleChannel.this.implCloseChannel();
                            } catch (IOException x) { }
                        }
                    }};
        }
        blockedOn(interruptor);
        Thread me = Thread.currentThread();
	//如果阻塞的IO线程已经中断,则记录中断阻塞IO操作线程的线程,
	//以便end方法判断,是否抛出ClosedByInterruptException
        if (me.isInterrupted())
            interruptor.interrupt(me);
    }

    /**
     * Marks the end of an I/O operation that might block indefinitely.
     *
     标志一个可能阻塞IO操作的结束
     *  This method should be invoked in tandem with the {@link #begin
     * begin} method, using a <tt>try</tt> ... <tt>finally</tt> block
     * as shown [url=#be]above[/url], in order to implement asynchronous
     * closing and interruption for this channel.  

     *
     * @param  completed
     *         <tt>true</tt> if, and only if, the I/O operation completed
     *         successfully, that is, had some effect that would be visible to
     *         the operation's invoker
     *
     * @throws  AsynchronousCloseException
     *          If the channel was asynchronously closed
     *
     * @throws  ClosedByInterruptException
     *          If the thread blocked in the I/O operation was interrupted
     */
    protected final void end(boolean completed)
        throws AsynchronousCloseException
    {
        blockedOn(null);
        Thread interrupted = this.interrupted;
	//如果中断线程为当前线程,则中断线程interrupted置空,抛出ClosedByInterruptException
        if (interrupted != null && interrupted == Thread.currentThread()) {
            interrupted = null;
            throw new ClosedByInterruptException();
        }
	//如果通道已关闭,且IO操作为完成,则抛出AsynchronousCloseException
        if (!completed && !open)
            throw new AsynchronousCloseException();
    }


    // -- sun.misc.SharedSecrets --
    static void blockedOn(Interruptible intr) {         // package-private
        sun.misc.SharedSecrets.getJavaLangAccess().blockedOn(Thread.currentThread(),
                                                             intr);
    }
}

总结:
AbstractInterruptibleChannel是一个可以异步关闭和中断IO阻塞线程的通道,所有具体的通道实现,如果想要可以异步关闭和中断,必须实现此类。AbstractInterruptibleChannel内部有一个Open布尔值用于表示通道是否打开。在通道关闭时调用implCloseChannel,implCloseChannel方法完成实际的关闭通道工作。有个中断处理器用于记录中断阻塞IO操作线程的线程,完成实际的关闭通道工作。有一组协调方法为begin和end方法,一般在一个可能阻塞的IO操作的开始调用begin,之后调用end方法,这些操作一般用一个try语句块,组合使用。begin方法主要初始化中断处理器,end方法根据IO操作是否完成和Open状态,及中断线程处理器,中断线程判断是抛出AsynchronousCloseException异常还是ClosedByInterruptException。


//Interruptible
package sun.nio.ch;
public interface Interruptible
{
    public abstract void interrupt(Thread thread);
}


//InterruptibleChannel,可异步关闭和中断的通道
package java.nio.channels;
import java.io.IOException;
/**
 * A channel that can be asynchronously closed and interrupted.
 *
InterruptibleChannel表示一个可以异步关闭和中断的通道。
 *  A channel that implements this interface is <i>asynchronously
 * closeable:</i> If a thread is blocked in an I/O operation on an
 * interruptible channel then another thread may invoke the channel's {@link
 * #close close} method.  This will cause the blocked thread to receive an
 * {@link AsynchronousCloseException}.
 *
 实现此接口的通道是异步可关闭:如果一个线程在一个可中断的通道,因为IO操作阻塞,
 其他线程可以调用close关闭通道。阻塞的线程将会接受到一个AsynchronousCloseException异常。
 * <p> A channel that implements this interface is also <i>interruptible:</i>
 * If a thread is blocked in an I/O operation on an interruptible channel then
 * another thread may invoke the blocked thread's {@link Thread#interrupt()
 * interrupt} method.  This will cause the channel to be closed, the blocked
 * thread to receive a {@link ClosedByInterruptException}, and the blocked
 * thread's interrupt status to be set.
 *
 实现此接口的通道是可中断的:如果一个线程在一个可中断的通道中,因为IO操作阻塞,
 其他线程可以调用阻塞的线程的中断方法Thread#interrupt,中断阻塞线程。中断后通道
 将会关闭,阻塞线程将会接受一个ClosedByInterruptException,阻塞线程的中断位将会被
 设置。
 * <p> If a thread's interrupt status is already set and it invokes a blocking
 * I/O operation upon a channel then the channel will be closed and the thread
 * will immediately receive a {@link ClosedByInterruptException}; its interrupt
 * status will remain set.
 *
 如果一个线程中断位已经被设置,因IO操作阻塞的线程所在的通道,将会关闭,阻塞线程
 将会立刻接受一个ClosedByInterruptException,阻塞线程仍处于中断状态。
 * <p> A channel supports asynchronous closing and interruption if, and only
 * if, it implements this interface.  This can be tested at runtime, if
 * necessary, via the <tt>instanceof</tt> operator.
 *
 InterruptibleChannel是一个异步中断和关闭的通道。在运行时环境中,如果需要,
 我们可以通过instanceof方法判断一个通道是否为InterruptibleChannel。
 *
 * @author Mark Reinhold
 * @author JSR-51 Expert Group
 * @since 1.4
 */

public interface InterruptibleChannel
    extends Channel
{

    /**
     * Closes this channel.
     *
     * <p> Any thread currently blocked in an I/O operation upon this channel
     * will receive an {@link AsynchronousCloseException}.
     关闭通道时,任何因IO操作在通道中,阻塞的线程将会接受一个AsynchronousCloseException
     * <p> This method otherwise behaves exactly as specified by the {@link
     * Channel#close Channel} interface.  

     *
     此方其他方面与Channel#close的方法作用基本相同。
     * @throws  IOException  If an I/O error occurs
     */
    public void close() throws IOException;
}
0
0
分享到:
评论

相关推荐

    mina:Java Nio Apache Mina Java Nio

    java-nio java-nio ...AbstractInterruptibleChannel, AbstractSelectableChannel, DatagramChannel, FileChannel, Pipe.SinkChannel, Pipe.SourceChannel, SelectableChannel, ServerSocketChannel, Socke

    99-智慧园区数据平台方案.pptx

    99-智慧园区数据平台方案.pptx

    node-v12.11.1-x86.msi

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    基于Springboot+Vue华强北商城二手手机管理系统-毕业源码案例设计.zip

    网络技术和计算机技术发展至今,已经拥有了深厚的理论基础,并在现实中进行了充分运用,尤其是基于计算机运行的软件更是受到各界的关注。加上现在人们已经步入信息时代,所以对于信息的宣传和管理就很关键。系统化是必要的,设计网上系统不仅会节约人力和管理成本,还会安全保存庞大的数据量,对于信息的维护和检索也不需要花费很多时间,非常的便利。 网上系统是在MySQL中建立数据表保存信息,运用SpringBoot框架和Java语言编写。并按照软件设计开发流程进行设计实现。系统具备友好性且功能完善。 网上系统在让售信息规范化的同时,也能及时通过数据输入的有效性规则检测出错误数据,让数据的录入达到准确性的目的,进而提升数据的可靠性,让系统数据的错误率降至最低。 关键词:vue;MySQL;SpringBoot框架 【引流】 Java、Python、Node.js、Spring Boot、Django、Express、MySQL、PostgreSQL、MongoDB、React、Angular、Vue、Bootstrap、Material-UI、Redis、Docker、Kubernetes

    Excel模版:工资条模板

    Excel工资条模板是一种预先设计好的电子表格文件,主要用于生成和打印员工的工资单,让员工清楚了解自己的工资组成和扣款详情。模板通常包含了以下几个关键部分: 1. **员工信息区**: - 姓名 - 员工编号/工号 - 部门 - 职位 2. **工资构成区**: - 基本工资 - 岗位工资 - 绩效奖金 - 加班工资 - 其他补贴(如交通补贴、餐补、全勤奖等) - 各项津贴(如高温补贴、取暖费等) - 其他应发收入(如年终奖、提成、福利等) 3. **扣款项目区**: - 社保扣款(养老保险、医疗保险、失业保险、工伤保险、生育保险) - 住房公积金 - 个人所得税 - 其他扣款(如迟到、旷工、违规罚款等) - 预借还款(如有) 4. **工资结算区**: - 应发工资总额 - 扣款总额 - 实发工资 5. **备注栏**: - 用于标注本月工资的特殊情况说明,如请假、调休、加班等情况。 6. **签名栏**: - 供员工确认工资数额无误后签名,也可以

    29-【智慧城市与政府治理分会场】10亿大数据助推都市治理-30页.pdf

    29-【智慧城市与政府治理分会场】10亿大数据助推都市治理-30页.pdf

    基于Springboot+Vue的租房管理系统-毕业源码案例设计.zip

    网络技术和计算机技术发展至今,已经拥有了深厚的理论基础,并在现实中进行了充分运用,尤其是基于计算机运行的软件更是受到各界的关注。加上现在人们已经步入信息时代,所以对于信息的宣传和管理就很关键。系统化是必要的,设计网上系统不仅会节约人力和管理成本,还会安全保存庞大的数据量,对于信息的维护和检索也不需要花费很多时间,非常的便利。 网上系统是在MySQL中建立数据表保存信息,运用SpringBoot框架和Java语言编写。并按照软件设计开发流程进行设计实现。系统具备友好性且功能完善。 网上系统在让售信息规范化的同时,也能及时通过数据输入的有效性规则检测出错误数据,让数据的录入达到准确性的目的,进而提升数据的可靠性,让系统数据的错误率降至最低。 关键词:vue;MySQL;SpringBoot框架 【引流】 Java、Python、Node.js、Spring Boot、Django、Express、MySQL、PostgreSQL、MongoDB、React、Angular、Vue、Bootstrap、Material-UI、Redis、Docker、Kubernetes

    线路工区光缆中断抢险预案.docx

    5G通信行业、网络优化、通信工程建设资料。

    299-教育数据资产管理平台及配套解决方案.pptx

    299-教育数据资产管理平台及配套解决方案.pptx

    太戈编程第345题答案

    abababababababab

    基于STM32F103C8单片机设计-旋转编码器数码管显示程序KEIL工程源码.zip

    STM32学习软件编程资料,STM32F103C8单片机经典外设应用设计实例软件源代码,KEIL工程文件,可供学习参考。

    5GKPI指标定义.pptx

    5G通信行业、网络优化、通信工程建设资料。

    全业务端到端-L2题库.xlsx

    5G通信行业、网络优化、通信工程建设资料

    3M 轨道砂光机精英系列说明书

    3M 轨道砂光机精英系列说明书

    基于Springboot+Vue教师工作量管理系统-毕业源码案例设计.zip

    网络技术和计算机技术发展至今,已经拥有了深厚的理论基础,并在现实中进行了充分运用,尤其是基于计算机运行的软件更是受到各界的关注。加上现在人们已经步入信息时代,所以对于信息的宣传和管理就很关键。系统化是必要的,设计网上系统不仅会节约人力和管理成本,还会安全保存庞大的数据量,对于信息的维护和检索也不需要花费很多时间,非常的便利。 网上系统是在MySQL中建立数据表保存信息,运用SpringBoot框架和Java语言编写。并按照软件设计开发流程进行设计实现。系统具备友好性且功能完善。 网上系统在让售信息规范化的同时,也能及时通过数据输入的有效性规则检测出错误数据,让数据的录入达到准确性的目的,进而提升数据的可靠性,让系统数据的错误率降至最低。 关键词:vue;MySQL;SpringBoot框架 【引流】 Java、Python、Node.js、Spring Boot、Django、Express、MySQL、PostgreSQL、MongoDB、React、Angular、Vue、Bootstrap、Material-UI、Redis、Docker、Kubernetes

    2023年亚太杯A题附件一,苹果图像数据集

    2023年亚太杯A题附件一,苹果图像数据集

    移动代维发电系统考试L2.xlsx

    5G通信、网络优化与通信建设

    59-《煤矿测量规程(1989版)》150.pdf

    59-《煤矿测量规程(1989版)》150.pdf

    施工现场安全技术交底模板.doc

    5G通信行业、网络优化、通信工程建设资料。

Global site tag (gtag.js) - Google Analytics