[doc] [java] Improve docs on NonThreadSafeSingleton

This commit is contained in:
Juan Martín Sotuyo Dodero
2017-01-30 16:18:57 -03:00
committed by Andreas Dangel
parent e45c10431f
commit 03306df325

View File

@ -1289,7 +1289,11 @@ Non-thread safe singletons can result in bad state changes. Eliminate
static singletons if possible by instantiating the object directly. Static static singletons if possible by instantiating the object directly. Static
singletons are usually not needed as only a single instance exists anyway. singletons are usually not needed as only a single instance exists anyway.
Other possible fixes are to synchronize the entire method or to use an Other possible fixes are to synchronize the entire method or to use an
initialize-on-demand holder class (do not use the double-check idiom). [initialize-on-demand holder class](https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom).
Refrain from using the double-checked locking pattern. The Java Memory Model doesn't
guarantee it to work unless the variable is declared as `volatile`, adding an uneeded
performance penalty. [Reference](http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html)
See Effective Java, item 48. See Effective Java, item 48.
</description> </description>