Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
April 12, 2002
In "Singletons with Needles and Thread," you declare the getInstance() method in the Singleton class as synchronized. The drawback to this approach: the getInstance() call is synchronized even if the Singleton exists -- thus introducing unneeded overhead.
I suggest the following as a better approach:
public class Singleton {
private static Singleton instance;
public static Singleton getInstance() {
if (null == instance) {
synchronized(Singleton.class) {
if (null == instance) {
instance = new Singleton();
}
}
}
return instance;
}
}
I'll keep this short: Double-checked locking is still broken. Please read:
Also, consider this alternative:
public class Singleton {
public final static Singleton INSTANCE = new Singleton();
private Singleton() {}
}