Is SharedSecrets thread-safe?

some-java-user-99206970363698485155 at some-java-user-99206970363698485155 at
Tue Dec 29 13:42:30 UTC 2020

the class `jdk.internal.access.SharedSecrets` provides getter methods which all look similar to this:
if (static_field == null) {
return static_field;

However, neither the static fields are `volatile` nor are the getter methods synchronized. So if my
understanding of the Java Memory Model is correct, the compiler is free to reorder the two static
field reads. So it is in theory possible that the first read yields a non-`null` value, but the
second read yields a `null` value which leads to incorrect behavior (for further reading [1]).

It is probably rather unlikely that this actually happens because `SharedSecrets` is in most cases
accessed from static initializers (which are only run once) and because not many classes access the
same `SharedSecrets` fields.

Is this analysis correct or did I forget to consider parts of the Memory Model logic, or is there
some JVM magic I am missing?

Kind regards


More information about the core-libs-dev mailing list