单例模式几种写法

本文摘自《多线程编程实战指南(核心篇)》

单例模式所要实现的目标(效果)非常简单:保持一个类有且仅有一个实例。出于性能的考虑,不少单例模式会采用延迟加载(Lazy Loading)的方式,即仅在需要用到相应实例的时候才创建实例。

单例模式 饿汉模式

 1 class SingleThreadSigleton{
 2     private static SingleThreadSigleton instance = new SingleThreadSigleton();
 3 
 4     SingleThreadSigleton(){
 5     }
 6 
 7     public static SingleThreadSigleton getInstance() {
 8         return instance;
 9     }
10 }

单线程单例 懒汉模式

 1 class SingleThreadSigleton{
 2     private static SingleThreadSigleton instance = null;
 3     
 4     private SingleThreadSigleton(){
 5     }
 6     
 7     public static SingleThreadSigleton getInstance(){
 8         if(null == instance){
 9             instance = new SingleThreadSigleton();
10         }
11         return instance;
12     }
13 }

简单加锁实现的单例模式实现

 1 class SingleThreadSigleton{
 2     private static SingleThreadSigleton instance = null;
 3 
 4     private SingleThreadSigleton(){
 5     }
 6     
 7     //静态函数加synchronized相当于synchronized(xxx.class) xxx表示当前类
 8     public static synchronized SingleThreadSigleton getInstance(){
 9         if(null == instance){
10             instance = new SingleThreadSigleton();
11         }
12         return instance;
13     }
14 }

基于双重检查锁定的错误单例模式实现

 1 class SingleThreadSigleton{
 2     private static SingleThreadSigleton instance = null;
 3 
 4     private SingleThreadSigleton(){
 5     }
 6     
 7     public static  SingleThreadSigleton getInstance(){
 8         if(null == instance){
 9             synchronized (SingleThreadSigleton.class) {
10                 if(null == instance) {
11                     instance = new SingleThreadSigleton();//操作1
12                 }
13             }
14         }
15         return instance;
16     }
17 }

因为需要考虑到重排序的因素,操作1可以分解为一下伪代码所示的几个独立子操作

objRef = allocate(SingleThreadSigleton.class);//分配空间
invokeConstructor(objRef);//初始化objRef引用的对象
instance = objRef;//将对象引用写入共享变量

根据重排序规则2和规则1:临界区内的操作可以在临界区内被重排序。因此上述操作可能被重排序为:子操作① -> 子操作③ -> 子操作②,这就可能导致程序出错。

基于双重检查锁定的正确单例模式实现

 1 class SingleThreadSigleton{
 2     private volatile static SingleThreadSigleton instance = null;
 3 
 4     private SingleThreadSigleton(){
 5     }
 6 
 7     public static  SingleThreadSigleton getInstance(){
 8         if(null == instance){
 9             synchronized (SingleThreadSigleton.class) {
10                 if(null == instance) {
11                     instance = new SingleThreadSigleton();
12                 }
13             }
14         }
15         return instance;
16     }
17 }

基于静态内部类的单例模式实现

 1 class SingleThreadSigleton{
 2 
 3     private SingleThreadSigleton(){
 4     }
 5 
 6     private static class InstanceHolder{
 7         final static SingleThreadSigleton INSTANCE = new SingleThreadSigleton();
 8     }
 9 
10     public static SingleThreadSigleton getInstance() {
11         return InstanceHolder.INSTANCE;
12     }
13 }

基于枚举类型的单例模式实现

 1 class EnumBasedSingletonExample{
 2     public static void main(String[] args) {
 3         new Thread(new Runnable() {
 4             @Override
 5             public void run() {
 6                 SingleThreadSigleton.INSTANCE.someService();
 7             }
 8         }).start();
 9     }
10 }
11 
12 enum  SingleThreadSigleton{
13     INSTANCE;
14     SingleThreadSigleton(){
15     }
16     public void someService(){
17         //doSomething
18     }
19 }

 单例模式防止反射、序列化、克隆的破坏

 1 import java.io.ByteArrayInputStream;
 2 import java.io.ByteArrayOutputStream;
 3 import java.io.ObjectInputStream;
 4 import java.io.ObjectOutputStream;
 5 import java.io.Serializable;
 6 import java.lang.reflect.Constructor;
 7 
 8 
 9 public class Main implements Serializable, Cloneable {
10     private static final long serialVersionUID = 6125990676610180062L;
11     private static Main main;
12     private static boolean isFristCreate = true;
13     private Main(){
14         /*防止反射破坏单例*/
15         if(isFristCreate){
16             synchronized (Main.class) {
17                 if(isFristCreate){
18                     isFristCreate = false;
19                 }
20             }
21         }else{
22             throw new RuntimeException("已经实例化一次, 不能再实例化");
23         }
24     }
25     
26     public  static Main getInstance(){
27         if (main == null) {
28             synchronized (Main.class) {
29                 if (main == null) {
30                     main = new Main();
31                 }
32             }
33         }
34         return main;
35     }
36     /*防止克隆破坏单例*/
37     @Override
38     protected Object clone() throws CloneNotSupportedException {
39         // TODO Auto-generated method stub
40         return main;
41     }
42     /*防止序列化破坏单例*/
43     private Object readResolve() {
44         // TODO Auto-generated method stub
45         return main;
46     }
47     
48     
49     public static void main(String[] args) throws Exception{
50         Main main = Main.getInstance();
51         System.out.println("singleton的hashCode:"+main.hashCode());
52         //通过克隆获取
53         Main clob = (Main) Main.getInstance().clone();
54         System.out.println("clob的hashCode:"+clob.hashCode()); 
55         //通过序列化,反序列化获取
56         ByteArrayOutputStream bos = new ByteArrayOutputStream();
57         ObjectOutputStream oos = new ObjectOutputStream(bos);
58         oos.writeObject(Main.getInstance());
59         ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
60         ObjectInputStream ois = new ObjectInputStream(bis);
61         Main serialize = (Main) ois.readObject();
62         if (ois != null) ois.close();
63         if (bis != null) bis.close();
64         if (oos != null) oos.close();
65         if (bos != null) bos.close();
66         System.out.println("serialize的hashCode:"+serialize.hashCode());
67         //通过反射获取
68         Constructor<Main> constructor = Main.class.getDeclaredConstructor();
69         constructor.setAccessible(true);
70         Main reflex = constructor.newInstance();
71         System.out.println("reflex的hashCode:"+reflex.hashCode());
72     }
73 }

 

posted @ 2019-09-22 18:01  乌乌乌龟  阅读(1182)  评论(0编辑  收藏  举报