其核心实现者,便是我们今天要深入剖析的AbstractInterruptibleChannel。 1.2AbstractInterruptibleChannel的设计目标AbstractInterruptibleChannel旨在为所有NIO通道提供一套统一的、高效的解决方案,以实现以下目标:异步关闭 为了达成这些目标,AbstractInterruptibleChannel巧妙地结合了对象监视器、CAS原子操作以及JVM内部的秘密武器。 AbstractInterruptibleChannel提供的异步关闭机制,正是实现快速优雅停机的关键一环。 AbstractInterruptibleChannel所提供的可靠中断能力,为上层响应式流的可取消性提供了坚实的保障。
public abstract class FileChannel extends AbstractInterruptibleChannel implements SeekableByteChannel AbstractInterruptibleChannel实现了InterruptibleChannel接口,interrupt大家都知道吧,用来中断线程执行的利器。 interrupted = target; try { AbstractInterruptibleChannel.this.implCloseChannel Thread.currentThread(); if (me.isInterrupted()) interruptor.interrupt(me); } 上面这段代码就是AbstractInterruptibleChannel
如果线程阻塞的是一个可以中断的channel,那么channel会被关闭,同时线程会收到java.nio.channels.ClosedByInterruptException,并且会设置中断标志 //AbstractInterruptibleChannel try { //关闭channel AbstractInterruptibleChannel.this.implCloseChannel wakeup,Selector立即返回 AbstractSelector.this.wakeup(); }}; } AbstractInterruptibleChannel.blockedOn
public abstract class FileChannel extends AbstractInterruptibleChannel implements SeekableByteChannel AbstractInterruptibleChannel实现了InterruptibleChannel接口,interrupt大家都知道吧,用来中断线程执行的利器。 上面这段代码就是AbstractInterruptibleChannel的核心所在。 首先定义了一个Interruptible的实例,这个实例中有一个interrupt方法,用来关闭Channel。
* throws IOException If an I/O error occurs */ public void close() throws IOException; } AbstractInterruptibleChannel try { //调用具体的实现关闭当前channel AbstractInterruptibleChannel.this.implCloseChannel
Channel |-AbstractInterruptibleChannel | |- FileChannel | |- FileChannelImpl |- ReadableByteChannelImpl 这是一个抽象类,此类继承AbstractInterruptibleChannel,实现了SeekableByteChannel、GatheringByteChannel、ScatteringByteChannel
源码简略如下: //该代码是 FileChannel 的关闭方法 (在FileChannel 的父类 AbstractInterruptibleChannel 中) public final end(n > 0); ...... } ....... } public abstract class AbstractInterruptibleChannel
interrupted = target; try { AbstractInterruptibleChannel.this.implCloseChannel
interrupted = target; try { // 释放锁关闭通道操作 AbstractInterruptibleChannel.this.implCloseChannel
OverridepublicvoidpostInterrupt(){AbstractSelector.this.wakeup();}};3.3begin()的三重防护展开代码语言:TXTAI代码解释protectedfinalvoidbegin(){AbstractInterruptibleChannel.blockedOn 3.4end()的清理语义展开代码语言:TXTAI代码解释protectedfinalvoidend(){AbstractInterruptibleChannel.blockedOn(null);}end 第六章:JDK25的现代演进与设计趋势6.1VarHandle的全面标准化AbstractSelector与AbstractSelectionKey、AbstractInterruptibleChannel
channel类结构.png 其中: AbstractInterruptibleChannel:NIO中可中断channel的基本实现,可参考 Java NIO中线程的中断机制 ReadableByteChannel
定义如下:package java.nio.channels;publicabstractclass FileChannel extends AbstractInterruptibleChannel
FileChannel 类结构 public abstract class FileChannel extends AbstractInterruptibleChannel implements
八、通道关闭源码 给予fileChannel.close()进行追踪 1.JDK源码追踪 代码位置:java.nio.channels.spi.AbstractInterruptibleChannel
. */ public abstract class SelectableChannel extends AbstractInterruptibleChannel implements
public abstract class FileChannel extends AbstractInterruptibleChannel implements SeekableByteChannel AbstractInterruptibleChannel实现了InterruptibleChannel接口,interrupt大家都知道吧,用来中断线程执行的利器。 interrupted = target; try { AbstractInterruptibleChannel.this.implCloseChannel Thread.currentThread(); if (me.isInterrupted()) interruptor.interrupt(me); } 上面这段代码就是AbstractInterruptibleChannel
需要调用channel.close() 最终调用的会使AbstractInterruptibleChannel的close方法 ? ? ?
public abstract class SelectableChannel extends AbstractInterruptibleChannel implements Channel {
inputStreamChannel.close(); } 3.1.2 分散读取和聚合写入 我们先看一下FileChannel的源码: public abstract class FileChannel extends AbstractInterruptibleChannel
这两个类是 AbstractInterruptibleChannel 和 AbstractSelectableChannel,它们分别为可中断的(interruptible)和可选择的(selectable FileChannel对象具有与file对象相同的访问权限,然后您就可以使用该通道对象来利用强大的FileChannel API了: public abstract class FileChannel extends AbstractInterruptibleChannel