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);
}
end方法的completed参数说明I / O操作是否实际完成,也就是说它是否具有对调用者可见的任何效果。 在读取字节的操作的情况下,例如,如果只有某些字节实际传输到调用者的目标缓冲区中,则该参数应为true 。
具体的通道类还必须以这样的方式实现implCloseChannel方法:如果在通道上的本地I / O操作中阻止另一个线程时被调用,则该操作将立即返回,通过抛出异常或正常返回。 如果一个线程被中断或被阻塞的通道被异步地关闭,则通道的end方法将抛出适当的异常。
这个类执行来实现所要求的同步Channel规范。 的的实现implCloseChannel方法不需要对同步可能被试图关闭通道的其他线程。
| Modifier | Constructor and Description |
|---|---|
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.