Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


Search Host

Monthly Fee($)
Disk Space (MB)
Register With us for Newsletter!
Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Have tutorials?
Add your tutorial to our Java Resource and get tons of hits.

We offer free hosting for your tutorials. and exposure for thousands of readers. drop a mail


Java Server Pages


Java Beans



Java Servlets



Java Code Examples

Interview Questions

Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

Web Promotion

Web Submission

Submit Sites

Manual Submission?

Web Promotion Guide

Hosting Companies

Web Hosting Guide

Web Hosting


Beginner Guide to Linux Server


Persistence Framework

Web Frameworks

Free EAI Tools

Web Servers

Aspect Oriented Programming

Free Proxy Servers


Adware & Spyware Remover

Open Source Softwares

Double-Checked locking


2002-12-07 The Java Specialists' Newsletter [Issue 061] - Double-checked locking

Author: Dr. Heinz M. Kabutz

If you are reading this, and have not subscribed, please consider doing it now by going to our subscribe page. You can subscribe either via email or RSS.

Welcome to the 61st edition of The Java(tm) Specialists' Newsletter sent to 5261 Java Specialists in 91 countries. Since my last newsletter I had the fortune of celebrating yet another birthday - try guess how old I am ;-)

Has your application ever caused an OutOfMemoryError? Would you like to know how you can receive a warning before that actually happens? Send me an email

2003 - Europe Design Patterns Tour Early in 2003 I will be travelling to Europe to present my Design Patterns Course in various countries. This 3 day course will teach you to be more effective as a Java/C++ programmer by examining many of the Gang-Of-Four Design Patterns that will help you write code that is reusable. At the very least, it will teach you a richer vocabulary for bringing your design ideas across to your colleagues. No more banging your head against the wall because you just cannot think of a good name for what you are trying to describe. The dates for the tour have not been fixed yet, but look to happen towards the end of January or beginning of February 2003.

  • Estonia looks like a good possibility with one bank already interested in an in-house course.
  • Munich is one of my favourite places in Germany, with lots of jovial Bavarians, and also some Germans. I would like to offer an open course here for anyone to attend. Please send me an email if you are thinking of attending.
  • London I will pass through on my way to the other cities. Do you work in London and are you eager to learn the new skills that Design Patterns will give you? I am specifically looking for companies that want to teach this skill to a number of their employees. I take a maximum of 12, a minimum of 8 for an in-house course. Please send me an email if you would like to host such an in-house course.
  • Double-checked Locking *yawn*

    No newsletter writer can claim that he is successful if he has not at least once written about double-checked locking and how it is theoretically broken. It is the ideal topic to write about. When you read my theoretical proofs, you will think I am very clever, thus enabling me to raise my rates next time you ask for help. At the same time, the problems described only happen on a fast multi-processor machine, and it is unlikely that you have one of those standing at home, so you cannot even contradict me.

    Let's take the Singleton pattern, described in the sample chapters of my Design Patterns Course (this is quite a large file due to the cool illustrative comics). If a Singleton does not need to be extendible, I write it like this:

    public class Singleton {
      private final static Singleton instance = new Singleton();
      public static Singleton getInstance() { return instance; }
      private Singleton() {}
      // other methods that you would need to do the actual work

    The class will be loaded when it is needed at which point the instance field is created.

    Another way of writing the Singleton is:

    public class Singleton {
      private static Singleton instance;
      public synchronized static Singleton getInstance() {
        if (instance == null) {
          instance = new Singleton();
        return instance;
      private Singleton() {}
      // other methods that you would need to do the actual work

    The advantage of that approach is that the Singleton instance is only created when you first use it. However, the class loader would usually take care of that aspect anyway, so we do not really win anything by complicating our lives like this. It does not win us anything, but at least it works.

    The problem with the new Singleton is that every time you call getInstance(), you will have to synchronize, even though you only really should need to the first time. Clever people have thus come up with this pattern:

    public class Singleton {
      private static Singleton instance;
      public static Singleton getInstance() {
        if (instance == null) {
          synchronized(Singleton.class) {
            if (instance == null) {
              instance = new Singleton();
        return instance;
      private Singleton() {}
      // other methods that you would need to do the actual work

    That mechanism is commonly called "Double-Checked Locking" or DCL for short. It is broken, at least in theory. In theory what may happen is that due to byte reordering, before the first thread has finished setting up the Singleton, i.e. has finished calling the constructor of the Singleton, the second thread already has a handle to it. In theory, therefore, it is possible that a thread can get a handle to a half-baked object using this mechanism.

    I have tried to gather some evidence that with the current JVM implementations the problem can occur. I have written test code and executed it on a friend's multi-processor. I was not successful in proving my assertions. I have spoken to a number of authors who have written about this problem, and they also do not have firm evidence that this occurs in the real world. I therefore offer no proof or even claim that what I wrote about in this newsletter is true.

    That is really all I have to say about the matter. Avoid the double-checked locking to avoid synchronization. In JDK 1.5 they are tidying up the memory model thus showing more clearly that DCL is broken. In the meantime, try not to be too clever ;-)

    Here is a challenge: I have not found a Singleton that is extendable in such a way that at compile time it is checked that there is only ever one instance. During my Design Patterns Course, I usually spend quite a bit of time on the Singleton, discussing things like why it is a pattern to avoid, what the differences between a Singleton and class methods are, etc. One of the issues we spend some time thinking about is the whole issue of making the Singleton extendable - I call it a polymorphic Singleton.

    That's it for this week.


This material from The Java(tm) Specialists' Newsletter by Maximum Solutions (South Africa). Please contact Maximum Solutions for more information.


Useful Links
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials


Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.