原文地址:http://www.journaldev.com/1377/java-singleton-design-pattern-best-practices-with-examples
Thread Safe Singleton:
The easier way to create a thread-safe singleton class is to make the global access method synchronized,
so that only one thread can execute this method at a time. General implementation of this approach is like the below class
|
package com.journaldev.singleton;
public class ThreadSafeSingleton {
private static ThreadSafeSingleton instance;
private ThreadSafeSingleton(){}
public static synchronized ThreadSafeSingleton getInstance(){
if (instance == null ){
instance = new ThreadSafeSingleton()
return instance;
}
}
Above implementation works fine and provides thread-safety but it reduces the performance
because
of cost associated with the synchronized method, although we need it only for the first few
threads who might create the separate instances (Read: Java Synchronization). To avoid this extra
overhead everytime, double
checked locking principle is used. In this approach, the synchronized block
is used inside the if condition
with an additional check to ensure that only one instance of singleton
class is created.
Below code snippet provides the double checked locking implementation.
public
static
ThreadSafeSingleton getInstanceUsingDoubleLocking(){
if
(instance ==
null
){
synchronized
(ThreadSafeSingleton.
class
) {
if
(instance ==
null
){
instance =
new
ThreadSafeSingleton();
}
}
}
return
instance;
}
Bill Pugh Singleton Implementation:Prior to Java 5, java memory model had a lot of issues and above approaches used to fail incertain scenarios where too many threads try to get the instance of the Singleton class simultaneously.So Bill Pugh came up with a different approach to create the Singleton class using ainner static helper class. The Bill Pugh Singleton implementation goes like this;package
com.journaldev.singleton;
public
class
BillPughSingleton {
private
BillPughSingleton(){}
private
static
class
SingletonHelper{
private
static
final
BillPughSingleton INSTANCE =
new
BillPughSingleton();
}
public
static
BillPughSingleton getInstance(){
return
SingletonHelper.INSTANCE;
}
}
Notice the private inner static class that contains the instance of the singleton class. When the singleton class is loaded, someone calls the getInstance method, this class gets loaded and creates the Singleton class instance. This is the most widely used approach for Singleton class as it doesn’t require synchronization. I am using this approach in many of my projects and it’s easy to understand and implement also. Using Reflection to destroy Singleton PatternReflection can be used to destroy all the above singleton implementation approaches. Let’s see this with an example class. public
class
EagerInitializedSingleton {
private
static
final
EagerInitializedSingleton instance =
new
EagerInitializedSingleton();
//private constructor to avoid client applications to use constructor
private
EagerInitializedSingleton(){}
public
static
EagerInitializedSingleton getInstance(){
return
instance;
}
}
public
class
ReflectionSingletonTest {
public
static
void
main(String[] args) {
EagerInitializedSingleton instanceOne = EagerInitializedSingleton.getInstance();
EagerInitializedSingleton instanceTwo =
null
;
try
{
Constructor[] constructors = EagerInitializedSingleton.
class
.getDeclaredConstructors();
for
(Constructor constructor : constructors) {
//Below code will destroy the singleton pattern
constructor.setAccessible(
true
);
instanceTwo = (EagerInitializedSingleton) constructor.newInstance();
break
;
}
}
catch
(Exception e) {
e.printStackTrace();
}
System.out.println(instanceOne.hashCode());
System.out.println(instanceTwo.hashCode());
}
}
If your singleton class is not using a lot of resources, this is the approach to use.
But in most of the scenarios, Singleton classes are created for resources such as File System,
Database connections etc and we should avoid the instantiation until unless client calls the
|