简体   繁体   English

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

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

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 .请注意,与Closeableclose()方法不同,此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()不同,如果多次调用则要求无效。 However, implementers of this interface are strongly encouraged to make their close methods idempotent.但是,强烈建议此接口的实现者使他们的 close 方法具有幂等性。


What do they mean by idempotent method and what are the side effects of calling this close() method twice?幂等方法是什么意思,两次调用此close()方法的副作用是什么?

And since interface Closeable extends AutoCloseable why are the side effects not to be seen in the close of Closeable interface?既然接口Closeable扩展了AutoCloseable为什么在Closeable接口的关闭中看不到副作用?

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 Closeable could 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.现在很明显,如果close()被调用一次或多次,所有通过isClosed()返回的状态将永远返回 true。 Therefore, the method close() would be considered idempotent.因此,方法close()将被认为是幂等的。

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: HTTP 请求示例:

  • Making a get request: If properly implemented then no matter how many times you make this request, you will get the same response.发出get请求:如果正确实施,那么无论您发出此请求多少次,您都会得到相同的响应。
  • 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!例如,一个非幂等的操作将发出创建资源的post请求 - 每次执行此操作时,您都会更改要发布到的应用程序的状态:每次都会创建一个新资源!

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

IDEMPOTENT Methods幂等方法

HTTP method is imdempotent method if the result become same for every call.如果每次调用的结果都相同,则 HTTP 方法是幂等方法。 When you call any request n times then result would be same.当您调用任何请求n 次时,结果将相同。

Take an example that adding ZERO in any number would be the same result.举个例子,在任何数字中添加都会得到相同的结果。

HTTP methods divided into two types. HTTP 方法分为两类。

  1. Safely repeatable ( Idempotent )安全可重复(幂等
  2. Can't be repeatable safely ( Non- Idempotent )不能安全地重复(非幂等

Safely repeatable ( Idempotent ) : Call method any times, it gives same result.安全可重复(幂等 :任何时候调用方法,它都会给出相同的结果。 it called Idempotent methods.它称为幂等方法。

GET, PUT, DELETE HTTP method called as Idempotent method. GET、PUT、DELETE HTTP 方法称为幂等方法。 Because因为

GET method : retrieving the resource. GET 方法:检索资源。 PUT method : It will update one resource. PUT 方法:它将更新一个资源。 DELETE method : It will used to delete the indiviual resource not impact other resource. DELETE 方法:用于删除单个资源,不影响其他资源。

Can't be repeatable safely ( Non- Idempotent )不能安全地重复(非幂等

But when POST method calls.但是当 POST 方法调用时。 POST method will create new resource everytime. POST 方法每次都会创建新资源。 Therefore it called as Non- Idempotent因此它被称为非幂等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么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? 为什么JMSProducer接口不扩展java.lang.Autocloseable? - Why JMSProducer interface does not extend java.lang.Autocloseable? Hibernate“资源类型Session没有实现java.lang.AutoCloseable” - Hibernate “The resource type Session does not implement java.lang.AutoCloseable” Java try-catch 连接中的错误“资源类型连接未实现 java.lang.AutoCloseable” - Java error "The resource type Connection does not implement java.lang.AutoCloseable" in the try-catch connection 为什么在Java 1.7中添加了java.lang.AutoCloseable接口 - Why java.lang.AutoCloseable interface is added in Java 1.7 Neo4j 3.5中的事务未实现java.lang.AutoCloseable - Transaction in Neo4j 3.5 does not implement java.lang.AutoCloseable AutoCloseable.close()方法是否违反Java的向后兼容规则 - Does AutoCloseable.close() method break Backward compatibilty rule of Java 找不到java.lang.AutoCloseable的类文件-ActiveMQ代码 - class file for java.lang.AutoCloseable not found - ActiveMQ code “this()”方法是什么意思? - What does "this()" method mean? 此方法是什么意思? - What does this Method mean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM