• Post Reply 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Double checked locking

 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java 1.4 and prior to that Double checked locking was broken but I heard sometime ago that from Java 1.5 it was fixed.

Now what is the general opinion on DCL given that I will have to use only synchronized keyword and no concurrent API.
 
Ranch Hand
Posts: 225
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Double-Checked Locking can be implemented in Java 5, if you do it just right. The general opinion seems to be that there are better ways to do it, though.

See http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html.
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code below will work; not that I made Helper volatile.
Effective Java 2nd edition ( Bloch ) has a nice section on this.

class Foo {
private volatile static Helper helper = null;
public Helper getHelper() {
if (helper == null)
synchronized(this) {
if (helper == null)
helper = new Helper();
}
return helper;
}
}
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a well known problem, and most of the "fixes" don't work.
See
http://www.ibm.com/developerworks/java/library/j-dcl.html
 
Kalyan Anand
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pat Farrell:
This is a well known problem, and most of the "fixes" don't work.
See
http://www.ibm.com/developerworks/java/library/j-dcl.html



The article you posted refers to 1.4 and prior. With 1.5 it is fixed. My questions was is there any valid reason NOT TO GO for DCL in Java 1.5
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://coderanch.com/t/100824/patterns/singleton-query
 
Kalyan Anand
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jelle Klap:
https://coderanch.com/t/100824/patterns/singleton-query



What you mean by lazy initialization holder approach ?
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This ensures that Foo is lazily initialized when the getFoo() method is first called, because of the implications of the static keyword in the context of class initialization. Also, because of static initilization there's no need for synchronization. See the JLS for more details, because these are all the details I remember, having read Java Concurrency in Practice a good while ago.
[ September 11, 2008: Message edited by: Jelle Klap ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic