Win a copy of Murach's Python Programming this week in the Jython/Python forum!
    Bookmark Topic Watch Topic
  • New Topic

So is double-checked locking really working in j2se 1.5?  RSS feed

 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Report post to moderator
I read that a rework of the "volatile" property in the Java 1.5 specs allows double-checked locking to work properly, meaning lazy initialization can be used in Singletons. Has that been confirmed?

Thank you,
Yuriy
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Report post to moderator
To the best of my knowledge, yes. At least, Jeremy Manson and Brian Goetz say so, and they're in a pretty good position to know. However if you really need a singleton, the Holder class idiom (discussed in the same link) seems to work better anyway.

Of course other issues with singletons still exist, like is global data a good thing, and what does "singleton" mean if there are multiple classloaders, or multiple JVMs in a distributed environment. See SingletonPattern for links to lots of discussion if you're not already familiar with these issues.
[ January 21, 2006: Message edited by: Jim Yingst ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Report post to moderator
LazyHolder is cute! I hadn't seen that before.
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Report post to moderator
Great, thanks!

I have a question about that whole read-write reordering.

Say we have a class:


Since s is not volatile, is it possible for it to ever be in a "partially constructed" state during the println() inside run()?

Thank you,
Yuriy
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Report post to moderator
Apparently this topic thread was closed on the Java Intermediate forum, and brought over here without any notification of the move. I already started a new thread in this (Threads) forum before noticing that my old thread is here. As such, a bartender should probably close this topic.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!