简体   繁体   English

java.time.Instant(1.8)是线程安全的吗?

[英]java.time.Instant (1.8) is thread safe?

Instant instant;
void updateBy(){
   instant = Instant.now();
}

if yes, How to prove Instant is thread safe? 如果是,如何证明Instant是线程安全的?

According to the docs for Instant under the implementation notes: 根据实施说明下的Instant文档:

This class is immutable and thread-safe. 此类是不可变的并且是线程安全的。

How to prove Instant is thread safe? 如何证明Instant是线程安全的?

Analyze (using sound mathematical / formal methods) the source code to prove that it meets all of the requirements for thread safety. 分析(使用合理的数学/形式方法)源代码,以证明其满足线程安全的所有要求。 That is the only way to prove something is thread-safe. 那是证明某些东西是线程安全的唯一方法。

You can't prove this by testing. 您无法通过测试证明这一点。

You can test non-thread-safe code any way you want and have the safety tests pass. 您可以按任何方式测试非线程安全代码,并通过安全测试。 But that doesn't prove anything. 但这并不能证明任何事情。 The tests may still fail on a different OS platform, or a different (possibly future) releases of Java. 在不同的OS平台或不同的Java版本(可能是将来的版本)上,测试仍可能失败。

(You can prove by testing is that something is NOT thread-safe. If the test shows undisputable symptoms of non-thread-safe behavior, that is an "existence proof".) (您可以通过测试来证明某些事情不是线程安全的。如果测试显示了非线程安全行为的无可争辩的症状,那就是“存在证明”。)

This should be moot for the Instant class. 对于Instant类,这应该是没有意义的。 The javadoc specifies that the Instant class is thread-safe. javadoc 指定 Instant类是线程安全的。 Unless you have a specific reason to believe that the class is not thread-safe (which would be a genuine JVM bug ) then it is advisable to not waste your time looking for things that probably don't exist. 除非您有特定的理由认为该类不是线程安全的(这将是一个真正的JVM bug ),否则建议不要浪费时间寻找可能不存在的东西。

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

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