I'm attempting to use Channels.newChannel
to wrap an InputStream in order to support interrupts. I've seen conflicting information whether this will work. Including the comment in ReadableByteChannelImpl: // Not really interruptible
In ReadableByteChannelImpl
, before making the the blocking call into InputStream.read
, AbstractInterruptibleChannel.begin
is called which sets up an new Interruptible
using sun.misc.SharedSecrets.getJavaLangAccess().blockedOn
which will close the wrapped 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);
}
Is it true that if an InputStream
will throw an IOException
from a blocked read call if it is closed by another thread then ReadableByteChannelImpl
will make the wrapped stream interruptible?