Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
    Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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

 
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
 
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 ]
 
(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.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!