简体   繁体   English

Java中如何保证Closeable接口的close()方法的幂等性?

[英]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 call close() all day and nothing will happen the second time and beyond.这意味着您可以整天调用close()并且第二次及以后不会发生任何事情。 [...] For classes that implement Closeable , 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 of Closeable , this close method is not required to be idempotent.请注意,与Closeableclose方法Closeable ,此 close 方法不需要是幂等的。 In other words, calling this close method more than once may have some visible side effect, unlike Closeable.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 的一个建议,对吗?

  1. 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()是幂等的并且扩展接口取消此要求时的抽象情况将只是一个糟糕的设计。

  2. 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.

相关问题 Closeable接口的close()方法签名是否有异常 - Does close() method of the Closeable interface has exception in the signature 为什么java.lang.AutoCloseable的close方法会抛出异常,但java.io.Closeable的close方法会抛出IOException? - Why close method of java.lang.AutoCloseable throws Exception, but close method of java.io.Closeable throws IOException? 为什么我收到“资源泄漏:”<unassigned Closeable value> 即使我在 Java 中使用 close() 方法也会出错? - Why I get 'Resource leak: '<unassigned Closeable value>' ' error even when I use close() method in Java? Java:在Closeable.close()中取消注册侦听器 - Java: Unregister listeners in Closeable.close() 如何在Java中组合Closeable对象? - How to compose Closeable objects in Java? java.io.Closeable:为什么在 close() 中抛出 RuntimeException 有效? - java.io.Closeable: why throwing RuntimeException in close() works? 可关闭的JTabbedPane - 关闭按钮的对齐方式 - Closeable JTabbedPane - alignment of the close button 在Java中对资源进行尝试是否安全-是否检查closeable是否不为null并在尝试关闭它时捕获异常? - Is it safe to use try with resources in Java - does it check if the closeable is not null and does it catch exceptions while trying to close it 具有Closeable参数的方法的单元测试 - Unit test for method with Closeable parameter 当垃圾收集发生时无法控制时,如何确保Java应用程序的性能? - How can the performance of java application be ensured when there is no control when garbage collection occurs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM