Java docs of close() method of java.lang.AutoCloseable says
Note that unlike the
close()
method of Closeable, thisclose()
method is not required to be idempotent. In other words, calling this close method more than once may have some visible side effect, unlikeCloseable#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?
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.
An idempotent method means that the result of a successful performed request is independent of the number of times it is executed.
Idempotence, in programming and mathematics, is a property of some operations such that no matter how many times you execute them, you achieve the same result. In programming, idempotence can be a property of many different code elements, including functions, methods, requests and statements.
Idempotency is important in APIs because a resource may be called multiple times if the network is interrupted. In this scenario, non-idempotent operations can cause significant unintended side-effects by creating additional resources or changing them unexpectedly.
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.
To adopt Einstein's aphorism, if you do the same thing, and get different results, then the method is not idempotent.
"Please sir, can I have a pay rise?" "No"
Same result every time. Asking for a pay rise is an idempotent operation.
get
request: If properly implemented then no matter how many times you make this request, you will get the same response. e.g. I make a get
request to view my bank balance and it's the same. everytime. Unlike the game monopoly, I've never had an extra $100 magically added to my bank balance.delete
request to destroy a resource - every time you do this you will be changing state of the application. For example, the IRS keeps making delete requests to my bank account - and every-time they do so the "state" of my bank account depletes and is halved. It is an idempotent operation.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With