[英]How is in Java the idempotence of the close() method of the Closeable interface ensured?
The Closeable
interface was introduced in Java 5 whereas the AutoCloseable
interface came in Java 7 together with the try-with-resources
statement. Closeable
接口是在 Java 5 中引入的,而AutoCloseable
接口是在 Java 7 中与try-with-resources
语句一起出现的。 Closeable
extends (since Java 7) the Autocloseable
interface. Closeable
扩展(自 Java 7 起) Autocloseable
接口。
In the book OCA/OCP Java SE 7 - Programmer I & II Study Guide it says on page 399:在OCA/OCP Java SE 7 - Programmer I & II Study Guide一书中,它在第 399 页说:
What happends if we call the
close()
multiple time?如果我们多次调用
close()
发生什么? It depends.这取决于。 For classes that implement
AutoCloseable
, the implementation is required to be idempotent.对于实现
AutoCloseable
类,要求实现是幂等的。 Which means you can callclose()
all day and nothing will happen the second time and beyond.这意味着您可以整天调用
close()
并且第二次及以后不会发生任何事情。 [...] For classes that implementCloseable
, there is no such guarantee.[...] 对于实现
Closeable
类,没有这样的保证。
So according to this text, implementations of AutoCloseable
need to be idempotent, and those of Closeable
not.所以根据本文,
AutoCloseable
实现需要是幂等的,而Closeable
不需要。 Now when I have a look at the documentation of the AutoCloseable
interface at docs.oracle.com , it says:现在,当我查看docs.oracle.com 上
AutoCloseable
接口的文档时,它说:
Note that unlike the
close
method ofCloseable
, this close method is not required to be idempotent.请注意,与
Closeable
的close
方法Closeable
,此 close 方法不需要是幂等的。 In other words, calling thisclose
method more than once may have some visible side effect, unlikeCloseable.close
which is required to have no effect if called more than once.换句话说,多次调用此
close
方法可能会产生一些可见的副作用,这与Closeable.close
不同,后者要求多次调用没有效果。
Now this is the opposite of what is written in the book.现在这与书中所写的相反。 I have two questions:
我有两个问题:
(1) What is correct? (1) 什么是正确的? The doc at docs.oracle.com or the book?
docs.oracle.com 上的文档还是这本书? Which of the two interfaces requires idempotence?
两个接口中哪个需要幂等性?
(2) No matter which one needs to be idempotent - am I right that Java has actually no way at all to ensure that it is idempotent? (2) 不管哪一个需要是幂等的——我对 Java 实际上根本没有办法确保它是幂等的吗? If so, the "requirement" of the
close
method to be idempotent is something that the programmer should do, but I can never be sure that someone who used the interface actually did do it, right?如果是这样,
close
方法的“要求”是幂等的,这是程序员应该做的事情,但我永远无法确定使用该接口的人确实做到了,对吗? In this case the idempotence is merely a suggestion of oracle, correct?在这种情况下,幂等性只是 oracle 的一个建议,对吗?
Javadoc from Oracle is correct.来自 Oracle 的 Javadoc 是正确的。 Just an intuition why -
AutoCloseable
objects are used in try(){}
(so called try with resources ) blocks, where close()
is actually called automatically and only once;只是一个直觉为什么 -
AutoCloseable
对象用于try(){}
(所谓的try with resources )块,其中close()
实际上是自动调用的,并且只调用一次; at the same time close()
from Closeable
interface method you always call manually and you can call it twice accidentally or to make your code easy to read.同时
close()
来自Closeable
接口方法,您总是手动调用,您可能会意外调用它两次或使您的代码易于阅读。 In addition - Closeable
extends AutoCloseable
and it shouldn't make the contract of close()
method from AutoCloseable
weaker, it can only add requirements.另外-
Closeable
扩展AutoCloseable
,它不应该使合同close()
从法AutoCloseable
弱,它只能增加的需求。 So, an abstract situation when AutoCloseable
required close()
to be idempotent and extended interface canceled this requirement would be just a bad design.因此,当
AutoCloseable
要求close()
是幂等的并且扩展接口取消此要求时的抽象情况将只是一个糟糕的设计。
Yes, your understanding is right.是的,你的理解是对的。 It's just a contract that a programmer should take into account.
这只是一个程序员应该考虑的契约。 Like the contract between
equals()
and hashCode()
.就像
equals()
和hashCode()
之间的契约。 You can implement it in an inconsistent way and a compiler or anything else will not flag it for you.您可以以不一致的方式实现它,编译器或其他任何东西都不会为您标记它。 The problem will materialize only in runtime.
问题只会在运行时出现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.