News for hackers. Stuff that matters.
Re:Using JDK 9 Memory Order Modes by bad_user on Wednesday March 15, 2017 @12:32PM (#13877314)
Scala's "lazy val" is using the double-checked locking pattern and I've never experienced problems with it (except for synchronizing on "this", but that's another issue).

In the example you're mentioning if "Thing" is built with finals or if it is synchronized, then it can't have problems.

And given that "Thing" in this sample is meant to be accessed by multiple threads, then if that access isn't synchronized, then the double-checked pattern is not going to be your only problem ;-)

Re:Using JDK 9 Memory Order Modes by _old_dude_ on Wednesday March 15, 2017 @02:08PM (#13878348)
Scala's lazy val uses the double-checked locking pattern with the field declared volatile, (the private field thing in the example) that's why there is no issue
Re:Using JDK 9 Memory Order Modes by bad_user on Wednesday March 15, 2017 @03:49PM (#13879180)
It doesn't matter if the field is volatile, it won't protect you in this case.

Basically when assigning a new object to a reference, the only way to know for sure that its internals are initialized before the assignment is to either work with finals or to synchronize that internal state.

Re:Using JDK 9 Memory Order Modes by _old_dude_ on Wednesday March 15, 2017 @05:50PM (#13880160)
if the field is volatile, you have an happen before relationship between the volatile read of thing before the synchronized block and the volatile write inside the synchronized block, so you have no way to see the object partially initialized.

It only works with the updated semantics of volatile (Java 5+), otherwise you're right.