Object Computing Inc have a good paper on their site giving an overview and code walkthrough using Hibernate, the open source persistence mechanism.
JBoss lawyers accuse Apache members of copying source code
The Geronimo project is Apache’s upcoming complete J2EE spec implementation.
Article on JSP2.0 new features
The JSP2.0 spec is due to be released soon, to be bundled as part of the new J2EE 1.4 spec.
OnJava.com have a good article discussing some of the new features in JSP2.0, which include:
- the JSTL Expression Language (EL) incorporated as standard into JSP2.0
- improved error/exception handling
- Custom tags can be defined in a JSP ‘tag file’ which declares the Custom Tag in terms of another JSP page, rather than subclassing the Custom Tag base classes
Singletons, Static Methods, and Double-checked Locking (and why to not use Double-checked Locking)
Overview | ||
This is brief discussion of the Singleton design pattern (refer to the GOF book, |
||
Why Singletons? | ||
Certain conditions may exist in a design that dictate that there should only ever be one instance of a particular Usually these classes represent or allow access to a limited resource, Two common solutions to implementing this shared class would be to implement:
|
||
Static methods | ||
Static methods (or Class methods) are associated with a particular Class, rather than an instance of a class. public class ClassA { public static int returnValue() { return 1; } } you can invoke the method like this: However there are drawbacks of static methods:
|
||
Overriding versus hiding | ||
Static methods cannot be overridden in subclasses which you could argue If you create a static method in subclass with the same name and signature Also, it is not valid to declare an instance method with the same signature Overriding and Hiding are discussed in further detail in the target=”_blank”>Java Language Specification. An example of this concept is here |
||
Implementing a Singleton (1) | ||
Assuming we have decided that the singleton pattern is a good solution for our problem, and that using static Heres the first example: public class SingletonA { private SingletonA instance; /** * Constructor is hidden so uncontrolled instances can not be created */ private SingletonA() {} public static SingletonA getInstance() { if(instance == null) { //create the singleton instance instance = new SingletonA(); } return instance; } } This appears to achieve what we need, however it is clearly not threadsafe. |
||
Implementing a Singleton – threadsafe attempt 1 | ||
If we need the instantiation of the single instance to be threadsafe then we need to introduce synchronization. public class SingletonB { private SingletonB instance; /** * Constructor is hidden so uncontrolled instances can not be created */ private SingletonB() {} public static synchronized SingletonB getInstance() { if(instance == null) { //create the singleton instance instance = new SingletonB(); } return instance; } } If we synchronize the whole The next refinement is to synchronize the least amount of code as possible (which is always good practice). |
||
Implementing a Singleton – threadsafe attempt 2, with Double-checked Locking |
||
For this refinement we synchronize only the lines of code that we want to be public class SingletonC { private SingletonC instance; /** * Constructor is hidden so uncontrolled instances can not be created */ private SingletonC() {} public static SingletonC getInstance() { if(instance == null) { synchronized { if(instance == null) { //create the singleton instance instance = new SingletonC(); } } } return instance; } } Heres where the ‘double-checked lock’ concept is introduced. The intent
Logically this is correct and will fulfill all our requirements. Unfortunately it is not guaranteed to work. Why? |
||
Double-checked Locking does not work | ||
Although the last solution looks good, it has been proven to not work in all
The reordering of bytecode statements is referred to as out of order Another problem with the double-checked locking is that in some circumstances |
||
Solutions | ||
According to David Bacon et al in their analysis of double-checked locking So how do you implement a threadsafe Singleton class in Java? Sometimes the
public class Singleton { private static Singleton instance = new Singleton(); /** * Constructor is hidden so uncontrolled instances can not be created */ private Singleton() {} public static SingletonC getInstance() { return instance; } } This avoids the need for synchronization, and the singleton instance will only |
||
References | ||
Other Related Articles
|