Java 中 InterruptibleChannel 接口扩展 Channel 接口的目的是什么?
What's the purpose of InterruptibleChannel interface extends Channel interface in Java?
我知道这听起来是个愚蠢的问题。但是由于 InterruptibleChannel 只声明了一种方法:
void close()
throws IOException
与频道界面中的name/signature相同。这样的接口继承的目的是什么?
这是为了在 Java 运行时进行反射吗?
它是相同的签名,但它被声明为也能够抛出 AsynchronousCloseException
(这也是一个 IOException
)。
实际上,这实际上是一个 "marker" 接口,虽然在功能上等同于使用 Channel
,但使用 InterruptibleChannel
可能会让任何阅读代码的人更清楚地了解它的用法。
目的是将可中断通道提供的合同直接编入类型系统。合同的保证被认为足够重要,足以证明这一点。
在InterruptibleChannel的文档中特别注意这句话:
A channel supports asynchronous closing and interruption if, and only if, it implements this interface. This can be tested at runtime, if necessary, via the instanceof
operator.
我知道这听起来是个愚蠢的问题。但是由于 InterruptibleChannel 只声明了一种方法:
void close()
throws IOException
与频道界面中的name/signature相同。这样的接口继承的目的是什么?
这是为了在 Java 运行时进行反射吗?
它是相同的签名,但它被声明为也能够抛出 AsynchronousCloseException
(这也是一个 IOException
)。
实际上,这实际上是一个 "marker" 接口,虽然在功能上等同于使用 Channel
,但使用 InterruptibleChannel
可能会让任何阅读代码的人更清楚地了解它的用法。
目的是将可中断通道提供的合同直接编入类型系统。合同的保证被认为足够重要,足以证明这一点。
在InterruptibleChannel的文档中特别注意这句话:
A channel supports asynchronous closing and interruption if, and only if, it implements this interface. This can be tested at runtime, if necessary, via the
instanceof
operator.