Double-checked locking more antipattern than pattern #650
Comments
|
@swhittaker is right. See in his first link, the authors of the declaration (Joshua Bloch, Doug Lea...). They're recognized by the Java community as Gurus, and they tell us the double check pattern is dangerous. Delete the page or put it in a new section called "antipatterns". |
|
Thanks for pointing this out. The articles seem credible and we should follow their advice. This affects Double Checked Locking pattern and one example of Singleton. We don't have antipatterns section so the correct action would be to delete those examples. I will accept PR that does it. |
|
I think it would be a shame to simply remove the examples. As it is a known pattern, and now established anti-pattern, I think it still belongs here, preserved in a new anti-pattern section as suggested by @pbougrier, with an explanation as to why it is not recommended for use. At the least, a Consequences header could be added to the pattern description (as done in singleton) with the reasons to not use it included, along with an explanation of the circumstances under which the pattern is valid. |
|
Yes, I guess we could do that (explain it should not be used) and add |
|
Guys, did you read the articles to the end? The Double Checked Locking was considered to be broken before JMM of java 1.5. In 1.5 they changed the semantics of |
|
@Auffahrend please note that the links also state that unless all the fields in the resource being protected are themselves immutable (final) then those fields also need to be declared volatile. To my mind, even if this works, having this restriction imposed on the fields of a class just to provide lazy loading when constructing it is untenable as it introduces maintenance and performance implications to that class for little gain. This isn't a case of "if we can" and more a case of "if we should". |
|
@swhittaker well, in case of "if we should" clearly describing pros and cons and requirements of pattern's implementation becomes even more important. |
|
Ok, so with a bit more research I've found this stackoverflow post where the accepted answer has an example for how to safely use DCL when in use for lazy loading. It seems like this may no longer be an anti-pattern (but the way in which it is often incorrectly implemented is and is worth a mention). The entry should be updated to note that there are better alternatives to DCL when working with a static resource, and to include the previously mentioned points around final fields / volatile fields. This is a good resource with multiple working and non-working examples of initialisation including DCL. However this is all to do with constructing an object, I've not seen a safe example of DCL where the value can be changed again after being set the first time (as in caching). |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

While some JVMs may do the expected thing, a memory model following the java specification may fail in multiple ways.
There are some way to make it work, such as using volatile and final field declarations.
The following links contained more detail:
https://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
https://www.javaworld.com/article/2074979/java-concurrency/double-checked-locking--clever--but-broken.html
The text was updated successfully, but these errors were encountered: