幂等方法是什么意思,调用java.lang.AutoCloseable的close方法有什么副作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19320115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:04:28  来源:igfitidea点击:

What does idempotent method mean and what are the side effects in case of calling close method of java.lang.AutoCloseable?

javaidempotentautocloseable

提问by Aniket Thakur

Java docs of close() method of java.lang.AutoCloseable says

java.lang.AutoCloseable 的 close() 方法的 Java 文档说

Note that unlike the close()method of Closeable, this close()method is not required to be idempotent. 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. However, implementers of this interface are strongly encouraged to make their close methods idempotent.

请注意,与Closeableclose()方法不同,此方法不需要是幂等的。换句话说,多次调用此 close 方法可能会产生一些可见的副作用,而 如果多次调用则需要无效。但是,强烈建议此接口的实现者使他们的 close 方法具有幂等性。close()Closeable#close()


What do they mean by idempotentmethod and what are the side effects of calling this close()method twice?


幂等方法是什么 意思,close()两次调用这个方法有什么副作用?

And since interface Closeableextends AutoCloseablewhy are the side effects not to be seen in the close of Closeableinterface?

既然接口Closeable扩展了AutoCloseable为什么在Closeable接口关闭时看不到副作用?

采纳答案by Edwin Buck

Idempotent means that you can apply the operation a number of times, but the resulting state of one call will be indistinguishable from the resulting state of multiple calls. In short, it is safe to call the method multiple times. Effectively the second and third (and so on) calls will have no visible effect on the state of the program.

幂等意味着您可以多次应用该操作,但一次调用的结果状态将与多次调用的结果状态无法区分。简而言之,多次调用该方法是安全的。实际上,第二次和第三次(以此类推)调用不会对程序状态产生明显影响。

So if you close this object once, and it closes, you don't have enough information to know if it is idempotent. However, if you close it twice, and the first time it closes, but the second time it throws an exception, it is clearly not idempotent. On the other hand, if you close it once, and close it twice, and the second closure results in the item remaining closed in the same manner (perhaps it is a noop), then it is idempotent.

因此,如果您关闭此对象一次,并且它也关闭了,您将没有足够的信息来知道它是否是幂等的。但是,如果关闭两次,第一次关闭,但是第二次抛出异常,显然不是幂等的。另一方面,如果您关闭它一次,然后关闭它两次,并且第二次关闭导致项目以相同的方式保持关闭(也许它是一个 noop),那么它是幂等的。

One technique of making an idempotent Closeablecould be:

制作幂等的一种技术Closeable可能是:

public class Example implements Closeable {

  private boolean closed;

  public Example() {
    closed = false;
  }

  public void close() {
    if (!isClosed()) {
      closed = true;
    }
  }

  public boolean isClosed() {
    return closed;
  }
}

Where it is now obvious that if close()is called once or multiple times, all returns of the state through isClosed()will forever return true. Therefore, the method close()would be considered idempotent.

现在很明显,如果close()被调用一次或多次,状态的所有返回都isClosed()将永远返回真。因此,该方法close()将被认为是幂等的。

回答by BKSpurgeon

Explanation of Concept Without Code

没有代码的概念解释

Einsteins definition of indempotency

爱因斯坦的幂等性定义

To adopt Einstein's aphorism, if you do the same thing, and get different results, then the method is not idempotent.

用爱因斯坦的格言,如果你做同样的事情,得到不同的结果,那么这个方法就不是幂等的。

Example of idempotency

幂等性的例子

"Please sir, can I have a pay rise?"

"No"

Same result every time. Asking for a pay rise is an idempotent operation.

每次结果都一样。要求加薪是一种幂等操作。

Examples with HTTP Requests:

HTTP 请求示例:

  • Making a getrequest: If properly implemented then no matter how many times you make this request, you will get the same response.
  • An operation that is not Idempotent, for example, would be making a postrequest to create a resource - every time you do this you will be changing state of the application you are posting this to: a new resource will be created every single time!
  • 制作一个get请求:如果正确实现,那么无论你有多少次提出这一要求,你会得到同样的反应。
  • 例如,一个非幂等的操作会发出post创建资源的请求 - 每次执行此操作时,您都会更改要发布到的应用程序的状态:每次都会创建一个新资源!

Answer to your question:

回答你的问题:

...there shouldn't be any side effects of closing twice if it is an idempotent method.....

...如果是幂等方法,关闭两次不应该有任何副作用.....

回答by Rahul Agrawal

JAVA GLOSSARY Idempotent

JAVA 词汇表 幂等

If methods are written in such a way that repeated calls to the same method do not cause duplicate updates, the method is said to be "idempotent."

如果方法的编写方式使得对同一方法的重复调用不会导致重复更新,则该方法被称为“幂等的”

In mathematics an idempotent element, or an idempotent for short, is anything that, when multiplied by itself, gives itself as result. For example, the only two real numbers which are idempotent are 0 and 1.

在数学中,幂等元素,或简称为幂等,是任何与自身相乘时,自身作为结果的东西。例如,仅有的两个幂等实数是 0 和 1。

In user interface design, a button can be called "idempotent" if pressing it more than once will have the same effect as pressing it once. For example, a "Pause" button is not idempotent if it toggles the paused state. On the other hand, if pressing it multiple times keeps the system paused and pressing "Play" resumes, then "Pause" is idempotent. This is useful in interfaces such as infrared remote controls and touch screens where the user may not be sure of having pressed the button successfully and may press it again. Elevator call buttons are also idempotent, though many people think they are not.

在用户界面设计中,如果一个按钮按下多次与按下一次具有相同的效果,则可以称为“幂等”。例如,如果“暂停”按钮切换暂停状态,则它不是幂等的。另一方面,如果多次按下它使系统暂停并恢复按下“播放”,则“暂停”是幂等的。这在诸如红外遥控器和触摸屏之类的界面中很有用,在这些界面中,用户可能不确定是否成功按下了按钮并可能再次按下。电梯呼叫按钮也是幂等的,尽管很多人认为它们不是。

Resource:-http://www.allapplabs.com/glossary/idempotent.htm

资源:- http://www.allapplabs.com/glossary/idempotent.htm