单例设计模式
Singleton是一种创建型模式,指某个类采用Singleton模式,则在这个类被创建后,只可能产生一个实例供外部访问,并且提供一个全局的访问点。
核心知识点如下:
(1) 将采用单例设计模式的类的构造方法私有化(采用private修饰)。
(2) 在其内部产生该类的实例化对象,并将其封装成private static类型。
(3) 定义一个静态方法返回该类的实例。
方法一就是传说的中的饿汉模式
优点是:写起来比较简单,而且不存在多线程同步问题,避免了synchronized所造成的性能问题;
缺点是:当类SingletonTest被加载的时候,会初始化static的instance,静态变量被创建并分配内存空间,从这以后,这个static的instance对象便一直占着这段内存(即便你还没有用到这个实例),当类被卸载时,静态变量被摧毁,并释放所占有的内存,因此在某些特定条件下会耗费内存。
/** *方法二 * 单例模式的实现:饱汉式,非线程安全 * */ public class SingletonTest { // 定义私有构造方法(防止通过 new SingletonTest()去实例化) private SingletonTest() { } // 定义一个SingletonTest类型的变量(不初始化,注意这里没有使用final关键字) private static SingletonTest instance; // 定义一个静态的方法(调用时再初始化SingletonTest,但是多线程访问时,可能造成重复初始化问题) public static SingletonTest getInstance() { if (instance == null) instance = new SingletonTest(); return instance; } }
方法二就是传说的中的饱汉模式
优点是:写起来比较简单,当类SingletonTest被加载的时候,静态变量static的instance未被创建并分配内存空间,当getInstance方法第一次被调用时,初始化instance变量,并分配内存,因此在某些特定条件下会节约了内存;
缺点是:并发环境下很可能出现多个SingletonTest实例。
/** *方法三 * 单例模式的实现:饱汉式,线程安全简单实现 * */ public class SingletonTest { // 定义私有构造方法(防止通过 new SingletonTest()去实例化) private SingletonTest() { } // 定义一个SingletonTest类型的变量(不初始化,注意这里没有使用final关键字) private static SingletonTest instance; // 定义一个静态的方法(调用时再初始化SingletonTest,使用synchronized 避免多线程访问时,可能造成重的复初始化问题) public static synchronized SingletonTest getInstance() { if (instance == null) instance = new SingletonTest(); return instance; } }
方法三为方法二的简单优化
优点是:使用synchronized关键字避免多线程访问时,出现多个SingletonTest实例。
缺点是:同步方法频繁调用时,效率略低。
/** * 方法四 * 单例模式最优方案 * 线程安全 并且效率高 * */ public class SingletonTest { // 定义一个私有构造方法 private SingletonTest() { } //定义一个静态私有变量(不初始化,不使用final关键字,使用volatile保证了多线程访问时instance变量的可见性,避免了instance初始化时其他变量属性还没赋值完时,被另外线程调用) private static volatile SingletonTest instance; //定义一个共有的静态方法,返回该类型实例 public static SingletonTest getIstance() { // 对象实例化时与否判断(不使用同步代码块,instance不等于null时,直接返回对象,提高运行效率) if (instance == null) { //同步代码块(对象未初始化时,使用同步代码块,保证多线程访问时对象在第一次创建后,不再重复被创建) synchronized (SingletonTest.class) { //未初始化,则初始instance变量 if (instance == null) { instance = new SingletonTest(); } } } return instance; } }
方法四为单例模式的最佳实现。内存占用低,效率高,线程安全,多线程操作原子性,又叫做DCL双检查锁机制,其实已经很好的解决了多线程下单列问题,同时也并非只有这一种方法,下面将延伸介绍其他方法。
package test;
/**
* 方法五
* 使用静态内置类实现单例模式
*/
public class SingletonTest {
private static class SingletonTestHandler{
private static SingletonTest singleton = new SingletonTest();
}
private SingletonTest(){}
public static SingletonTest getsingleton(){
return SingletonTestHandler.singleton;
}
}
package test;
public class MyThread extends Thread {
public void run(){
System.out.println(SingletonTest.getsingleton().hashCode());
}
}
package test;
public class Test {
public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
MyThread t3 = new MyThread();
t1.start();
t2.start();
t3.start();
}
}
输出结果为:
1698032342
1698032342
1698032342
方法五实现了多线程下单例模式,但接下来方法6将看到在序列化和反序列化对象单例中的问题
package test;
import java.io.Serializable;
/**
* 方法六
* 序列化与反序列化的单例模式实现
*/
public class SingletonTest implements Serializable {
private static final long serialVersionUID = 888L;
private static class SingletonTestHandler{
private static SingletonTest singleton = new SingletonTest();
}
private SingletonTest(){}
public static SingletonTest getsingleton(){
return SingletonTestHandler.singleton;
}
}
package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class Test {
public static void main(String[] args) {
try {
SingletonTest singleton = SingletonTest.getsingleton();
FileOutputStream fosRef = new FileOutputStream(new File("SingletonTest.txt"));
ObjectOutputStream oosRef = new ObjectOutputStream(fosRef);
oosRef.writeObject(singleton);
oosRef.close();
fosRef.close();
System.out.println(singleton.hashCode());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
FileInputStream fisRef = new FileInputStream(new File("SingletonTest.txt"));
ObjectInputStream iosRef = new ObjectInputStream(fisRef);
SingletonTest singleton = (SingletonTest) iosRef.readObject();
iosRef.close();
fisRef.close();
System.out.println(singleton.hashCode());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
输出结果:865113938
1442407170
很明显不是同一个实例,为了解决这个问题 需要在 对象SingletonTest加上以下方法,在进行反序列化时会自动调用,这样就会保证同一个实例
protected Object readResolve() throws ObjectStreamException{
System.out.println("调用了readResovle方法!");
return SingletonTestHandler.singleton;
}
输出:
865113938
调用了readResovle方法!
865113938
package test;
import java.io.ObjectStreamException;
import java.io.Serializable;
/**
* 方法七
* 使用static代码块实现单例模式
*/
public class SingletonTest {
private static SingletonTest singleton = null;
private SingletonTest(){
}
static {
singleton = new SingletonTest();
}
public static SingletonTest getsingleton(){
return singleton;
}
}
package test;
public class MyThread extends Thread {
public void run(){
for(int i = 0; i < 5; i++){
System.out.println(SingletonTest.getsingleton().hashCode());
}
}
}
package test;
public class Test {
public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
MyThread t3 = new MyThread();
t1.start();
t2.start();
t3.start();
}
}
输出: