On Fri, Jun 3, 2011 at 12:24 PM, Chris <[email protected]> wrote:
> Read "Double-Checked Locking; Clever but Broken"
>
> http://www.javaworld.com/javaworld/jw-02-2001/jw-0209-double.html
>
> The pattern, while common, was broken prior to Java 5 and was fixed after
> but only if mySingleton is declared volatile.

More on this here:

http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html

The above article is what reminded me that I had forgotten about the
volatile keyword in my original WakefulIntentService. See the "Fixing
Double-Checked Locking using Volatile" section.

The "static helper inner-class" and similar static initialization
approaches work well when there's no configuration necessary, and I
use those in many places for managing API level-dependent stuff in
Android. In my WakefulIntentService case, though, I am initializing a
WakeLock, and so I need a Context to get the PowerManager system
service. Hence, double-checked locking with volatile is, AFAIK, as
good as it gets for that scenario, modulo perhaps using synchronized()
per Ms. Hackborn.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

Android 3.0 Programming Books: http://commonsware.com/books

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to