Java Reference
In-Depth Information
Listing 16.5. Eager Initialization.
Using eager initialization, shown in Listing 16.5 , eliminates the synchronization cost incurred
on each call to getInstance in SafeLazyInitialization . This technique can be
combined with the JVM's lazy class loading to create a lazy initialization technique that does
not require synchronization on the common code path. The lazy initialization holder class
idiom [EJ Item 48] in Listing 16.6 uses a class whose only purpose is to initialize the Re-
source . The JVM defers initializing the ResourceHolder class until it is actually used
[JLS 12.4.1], and because the Resource is initialized with a static initializer, no addition-
al synchronization is needed. The first call to getResource by any thread causes Re-
sourceHolder to be loaded and initialized, at which time the initialization of the Re-
source happens through the static initializer.
Listing 16.6. Lazy Initialization Holder Class Idiom.
16.2.4. Double-checked Locking
No book on concurrency would be complete without a discussion of the infamous double-
checked locking (DCL) antipattern, shown in Listing 16.7 . In very early JVMs, synchron-
ization, even uncontended synchronization, had a significant performance cost. As a result,
Search WWH ::




Custom Search