首页 > 其他 > 详细

三种单例模式的实现方式

时间:2014-04-23 08:42:02      阅读:489      评论:0      收藏:0      [点我收藏+]

本示例主要演示了三种单例模式的实现方式,并对各种实现方式的优缺点进行比较。
1,静态变量直接创建单例对象,
2,延迟加载, 由于要保证线程安全,使用了synchronized来保证创建过程的线程同步
3,使用内部类

bubuko.com,布布扣
bubuko.com,布布扣
 1 /**
 2  * 本示例主要演示了三种单例模式的实现方式,并对各种实现方式的优缺点进行比较。
 3  * 1,静态变量直接创建单例对象,
 4  * 2,延迟加载, 由于要保证线程安全,使用了synchronized来保证创建过程的线程同步
 5  * 3,使用内部类
 6  */
 7 package Pattern;
 8 
 9 import java.util.concurrent.ExecutorService;
10 import java.util.concurrent.Executors;
11 
12 /**
13  * 单例模式
14  * @author LPX
15  *
16  */
17 public class StaticSingleton {
18     private StaticSingleton(){
19         System.out.println("StaticSingleton creating.");
20     }
21     /**
22      * 
23      * @author LPX
24      *
25      */
26     private static class SingletonHolder{
27         static{
28             System.out.println("SingletonHolder static statements.");
29         }
30         //Holder在该类中只会创建一次,并且默认对多线程是友好的。
31         public static StaticSingleton Holder=new StaticSingleton();
32     }
33     
34     public static StaticSingleton getInstance(){
35         return StaticSingleton.SingletonHolder.Holder;
36     }
37     
38     //在类进行加载时,就会进行创建,比如,在使用StaticSingleton.other()方法时,就会进行加载。
39     private static StaticSingleton instance=new StaticSingleton();
40     public static StaticSingleton getInstance3(){
41         return instance;
42     }
43     
44     private static StaticSingleton lazyInstance=null;
45     /**
46      * 需要使用同步机制synchronized来保证对lazyInstance的判断与赋值是线程安全
47      * 在该方法中由于要进行数据同步,所以在多线程的环境中性能会有较低
48      * @return
49      */
50     public static synchronized StaticSingleton getInstance2(){
51         if(lazyInstance==null){
52             lazyInstance=new StaticSingleton();
53         }
54         return lazyInstance;
55     }
56     
57     public static void other(){
58         System.out.println("Other");
59     }
60     
61     public static void main(String[] args){
62         final long start=System.currentTimeMillis();
63         
64         Runnable runable=new Runnable(){
65             @Override
66             public void run() {
67                 long start=System.currentTimeMillis();
68                 //long start=System.currentTimeMillis();
69                 for(int i=0;i<100000;i++){
70                     StaticSingleton.getInstance2();
71                 }
72                 System.out.println("多线程用时:"+(System.currentTimeMillis()-start));
73             }
74         };
75         /*
76         ExecutorService exe=Executors.newFixedThreadPool(1);
77         exe.submit(new Thread(runable));
78         exe.submit(new Thread(runable));
79         exe.submit(new Thread(runable));
80         exe.submit(new Thread(runable));
81         exe.submit(new Thread(runable));
82         exe.submit(new Thread(runable));*/
83         StaticSingleton.other();
84 
85         for(int i=0;i<100000;i++){
86             StaticSingleton.getInstance3();
87         }
88         System.out.println("用时:"+(System.currentTimeMillis()-start));
89     }
90 }
View Code
bubuko.com,布布扣

 

三种单例模式的实现方式,布布扣,bubuko.com

三种单例模式的实现方式

原文:http://www.cnblogs.com/bjwylpx/p/3680678.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!