java.time.Instant (1.8) is thread safe?

1.8k views Asked by At
Instant instant;
void updateBy(){
   instant = Instant.now();
}

if yes, How to prove Instant is thread safe?

2

There are 2 answers

1
GBlodgett On BEST ANSWER

According to the docs for Instant under the implementation notes:

This class is immutable and thread-safe.

6
Stephen C On

How to prove Instant is thread safe?

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.

(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. The javadoc specifies that the Instant class is thread-safe. 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.