The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Thread-safe singleton in java

Hi I've been looking into how to provide singleton access to my underlying data layer for application logic.  This design pattern is fairly proven I would have thought.  But all the allegedly thread-safe methods I have found on articles from the net have been disputed in subsequent feedback/comments.

Is there a universally accepted way of making singletons thread-safe in java?  I'd have thought this would have been sorted definitively by now.  Also, mutator methods will be needed as it is the data access layer...
Entropy Send private email
Sunday, June 11, 2006
 
 
Avoid any implementations that use the double-checked locking pattern because it doesn't work. "Effective Java" by Bloch gives a couple of approaches for implementing singletons:

public class Elvis {
  public static final Elvis INSTANCE = new Elvis();

  private Elvis() {
    ...
  }
}

For the second approach, make the final class variable private and provide a public static getInstance method that returns INSTANCE. This has the advantage that you can make Elvis a non-singleton later without breaking the class's API. Both of these implementations are thread-safe.
John Topley Send private email
Sunday, June 11, 2006
 
 
Thanks for that.  I am aware of the Double Checked Locking anti-pattern and why it fails.  Maybe you can help me with a query related to the solution you just posted:

If I have further service methods, that do not access class-scoped variables (ie all variables are declared inside the method or passed as parameters), then are these variables thread-safe?  I have heard arguments either way, that yes they are (two threads calling the same service method will have two different copies of the method-scoped variables) or no they are not (two threads do not make individual copies unless ThreadLocal is used).

Which is it?
Entropy Send private email
Sunday, June 11, 2006
 
 
Local variables (variables declared and used within a method), method parameters and return values are all thread-safe because they reside on the stack. See http://www.artima.com/designtechniques/threadsafety.html
John Topley Send private email
Sunday, June 11, 2006
 
 
Thats what I wanted to hear :) cheers
Entropy Send private email
Sunday, June 11, 2006
 
 
namehere
Sunday, June 11, 2006
 
 
If the singleton is stateful (and non-threadsafe) then something like this may work:

public class Elvis {
  private static final Elvis INSTANCE = new Elvis();

  private Elvis() {
    ...
  }

  public static void rockAndRoll() {
      synchronized(INSTANCE) {
        // Non-thread safe code goes here
      }
  }     
}
Dino Send private email
Monday, June 12, 2006
 
 
Double-checked lock is back!  Works in 1.5

http://www-128.ibm.com/developerworks/java/library/j-jtp03304/
Lenny
Wednesday, June 14, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz