Chris Webster's Weblog
Double Check locking Part 2 / Using AtomicReference
When I posted the original results of some of the solutions for multi-threaded lazy initialization, there was an additional suggestion about using the AtomicReference class. The code would be something like:private static AtomicReferenceThis approach about the same as the volatile solution (the results can be found http://blogs.sun.com/roller/page/cwebster?entry=double_check_locking. The summary is about a 3% penalty over a straight method call but about 6% better performance than fully synchronizing the methods.instance = new AtomicReference(); public static AtomicReferenceSingleton getDefault() { AtomicReferenceSingleton ars = instance.get(); if (ars == null) { instance.compareAndSet(null, new AtomicReferenceSingleton()); ars = instance.get(); } return ars; }
Posted at 06:59PM May 27, 2006 by Christopher Webster in Java | Comments[2]
Saturday May 27, 2006
Posted by Tom Hawtin on May 28, 2006 at 08:33 AM PDT #
Posted by Mridul on May 30, 2006 at 08:54 PM PDT #