What does idempotent method mean and what are the

2020-02-02 11:34发布

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

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.


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

And since interface Closeable extends AutoCloseable why are the side effects not to be seen in the close of Closeable interface?

3条回答
虎瘦雄心在
2楼-- · 2020-02-02 12:13

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.

One technique of making an idempotent Closeable could be:

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.

查看更多
一纸荒年 Trace。
3楼-- · 2020-02-02 12:14

JAVA GLOSSARY Idempotent

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.

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

查看更多
对你真心纯属浪费
4楼-- · 2020-02-02 12:26

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:

  • Making a get request: 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 post request 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!

Answer to your question:

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

查看更多
登录 后发表回答