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

后端 未结 3 675
予麋鹿
予麋鹿 2020-12-08 02:50

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

Note that unlike the close() method of Closeable, this close() method is no

3条回答
  •  臣服心动
    2020-12-08 03:13

    Explanation of Concept Without Code

    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!

提交回复
热议问题