简体   繁体   English

是 java.util.Optional<t> map function 线程安全? 如果是,我该如何测试它?</t>

[英]Is java.util.Optional<T> map function thread-safe ? If yes, How can I test it?

I do not have a lot of experience in Java and I have not used java.util.Optional map function in production code. I do not have a lot of experience in Java and I have not used java.util.Optional map function in production code. However, I need to use it for implementing a use case and the first thought which came to my mind, is this thread-safe?但是,我需要用它来实现一个用例,我想到的第一个想法是,这是线程安全的吗? As long as the mapping function in the argument is thread-safe, can we assume the overall operation will be thread safe?只要参数中的映射 function 是线程安全的,我们可以假设整体操作是线程安全的吗? How can I test this?我该如何测试呢?

I appreciate the help.我很感激帮助。

Thank you.谢谢你。

Optional.map is as thread-safe as the function you pass in as the parameter. Optional.map与作为参数传入的 function 一样是线程安全的。 Essentially all it does is:基本上它所做的就是:

public <V> Optional<V> map(Function<? super T, ? extends V> fn) {
  if (isPresent()) {
    V value = fn.apply(get());
    if (value != null) {
      return Optional.of(value);
    }
  }
  return Optional.absent();
}

Since Optional is immutable (meaning that isPresent() and get() always return the same value, and the value returned by get() will have been safely published), the only thing here that could possibly be thread-unsafe is the call fn.apply(get()) .由于Optional是不可变的(这意味着isPresent()get()总是返回相同的值,并且get() () 返回的值将被安全地发布),这里唯一可能是线程不安全的就是调用fn.apply(get())


As long as the mapping function in the argument is thread-safe, can we assume the overall operation will be thread safe?只要参数中的映射 function 是线程安全的,我们可以假设整体操作是线程安全的吗? How can I test this?我该如何测试呢?

With great difficulty.好容易。 You can't really write a test to prove that something is thread-safe, because you can't prove the reason you didn't observe thread-unsafety is because of the correctness of your code, or because your test simply didn't do anything that would exercise the thread-unsafety.您不能真正编写测试来证明某些东西是线程安全的,因为您无法证明您没有观察到线程不安全的原因是因为您的代码的正确性,或者因为您的测试根本没有做任何会行使线程不安全的事情。

You can only do this by reasoning about the code with respect to the properties of the Java Memory Model.您只能通过推理与 Java Memory Model 的属性有关的代码来做到这一点。

In a couple of situations I have been able to demonstrate the thread-unsafety of a class or method by spawning two or more threads calling the method repeatedly until one either gets a wrong result or an exception.在几种情况下,我已经能够证明 class 或方法的线程不安全性,方法是产生两个或多个重复调用该方法的线程,直到其中一个得到错误结果或异常。 As Andy Turner already said, even if the method is thread-unsafe, there is no guarantee that your test will reveal it.正如 Andy Turner 已经说过的,即使该方法是线程不安全的,也不能保证您的测试会揭示它。 In some situations it will in practice.在某些情况下,它会在实践中。

Since Optional is immutable, it is thread-safe.由于Optional是不可变的,因此它是线程安全的。 So in this case your test will not show anything (unless the function you pass to map() is thread-unsafe).因此,在这种情况下,您的测试不会显示任何内容(除非您传递给map()的 function 是线程不安全的)。 So you will have to decide yourself whether that leaves you more assured than you already were.所以你必须自己决定这是否让你比以前更放心。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM