Java ReadableByTechannel如何处理中断

Java ReadableByTechannel如何处理中断,java,Java,我正在尝试使用通道.newChannel包装输入流以支持中断。我看到了关于这是否有效的相互矛盾的信息。包括ReadableByteChannelImpl中的注释://实际上不可中断 在ReadableByteChannelImpl中,在对InputStream.read进行阻塞调用之前,调用abstractinterruptablechannel.begin,使用sun.misc.SharedSecrets.getJavaLangAccess().blockedOn设置一个新的interrupt

我正在尝试使用
通道.newChannel
包装输入流以支持中断。我看到了关于这是否有效的相互矛盾的信息。包括ReadableByteChannelImpl中的注释:
//实际上不可中断

ReadableByteChannelImpl
中,在对
InputStream.read
进行阻塞调用之前,调用
abstractinterruptablechannel.begin
,使用
sun.misc.SharedSecrets.getJavaLangAccess().blockedOn
设置一个新的
interruptable
,它将关闭包装的InputStream

protected final void begin() {
    if (interruptor == null) {
        interruptor = new Interruptible() {
                public void interrupt(Thread target) {
                    synchronized (closeLock) {
                        if (!open)
                            return;
                        open = false;
                        interrupted = target;
                        try {
                            AbstractInterruptibleChannel.this.implCloseChannel();
                        } catch (IOException x) { }
                    }
                }};
    }
    blockedOn(interruptor);
    Thread me = Thread.currentThread();
    if (me.isInterrupted())
        interruptor.interrupt(me);
}

如果一个
InputStream
将从一个阻塞的读取调用中抛出一个
IOException
,如果它被另一个线程关闭,那么
ReadableByteChannelImpl
将使封装的流可中断,这是真的吗?

是的,非常正确。
Channels.newChannel()
适配器返回一个
ReadableByteChannelImpl
实例,除非流是
FileInputStream
;在这种情况下,它返回一个
文件通道
,该通道也是可中断的

当您通过Techannel获得一个可读取的
时,您已经检查过的代码表明(并通过测试确认)底层
InputStream
被读取中阻塞的线程上的
中断()
异步关闭

它可以依赖于
InputStream
的实现,但是核心Java运行时中包含的实现将通过在读取线程中抛出异常来响应异步闭包。例外情况的具体类型会有所不同