public abstract class AbstractInterruptibleChannel extends Object implements Channel, InterruptibleChannel
这个类封装了需要实现信道的异步关闭和中断低级机械。一个具体的通道类必须调用begin
和end
方法之前和之后,分别调用I/O操作可能会无限期的阻塞。为了确保end
总是调用方法,这些方法应在一个try …用 finally块:
boolean completed = false; try { begin(); completed = ...; // Perform blocking I/O operation return ...; // Return result } finally { end(completed); }
的completed争论的end
方法告诉是否实际完成的I/O操作,即,是否有任何影响,将给调用者可见。在一个操作读取字节,该案为例,这个说法应该是true如果只有几个字节,实际上是转移到调用者的目标缓冲区。
一个具体的通道类也必须贯彻implCloseChannel
方法在这样一种方式,如果是调用另一个线程被阻塞在本地I/O操作在通道那操作将立即返回,或者抛出一个异常或恢复正常。如果一个线程被中断或通道的堵塞是异步关闭通道的end
方法将抛出相应的异常。
这类执行需要实现Channel
规范同步。该方法的实现不需要同步implCloseChannel
对其他线程可能会试图关闭通道。
Modifier | Constructor and Description |
---|---|
protected |
AbstractInterruptibleChannel()
初始化该类的一个新实例。
|
protected AbstractInterruptibleChannel()
public final void close() throws IOException
如果通道已被关闭,则此方法将立即返回。否则,它标志着通道关闭,然后调用implCloseChannel
方法为了完成关闭操作。
close
接口
Closeable
close
接口
AutoCloseable
close
接口
Channel
close
接口
InterruptibleChannel
IOException
如果I/O错误发生
protected abstract void implCloseChannel() throws IOException
这种方法是以close
方法调用执行关闭通道的实际工作。此方法只被调用,如果通道还没有被关闭,它永远不会被调用不止一次。
此方法的一个实现,必须安排任何其他线程被阻塞在这个通道上的I / O操作立即返回,无论是通过抛出一个异常或返回正常。
IOException
如果I/O错误时关闭通道
public final boolean isOpen()
Channel
protected final void begin()
protected final void end(boolean completed) throws AsynchronousCloseException
completed
-
true如果,仅仅是如果,I/O操作成功完成,即产生了一定的影响,会对操作的调用可见
AsynchronousCloseException
如果通道异步关闭
ClosedByInterruptException
-如果该线程在I/O操作阻塞中断
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.