• 三种单例模式的实现方式


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

     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
  • 相关阅读:
    【BZOJ5306】染色(HAOI2018)-容斥原理+NTT
    【BZOJ3129】方程(SDOI2013)-容斥原理+扩展Lucas定理
    【BZOJ3876】支线剧情(AHOI&JSOI2014)-有上下界费用流
    【POJ1149】PIGS-最大流+优化建模
    【BZOJ1941】Hide and Seek(SDOI2010)-KD树
    【BZOJ1834】网络扩容(ZJOI2010)-最大流+费用流+拆边
    【BZOJ1927】星际竞速(SCOI2010)-费用流+拆点
    【BZOJ4872】分手是祝愿(六省联考2017)-期望DP
    【BZOJ2879】美食节(NOI2012)-费用流+拆点+动态加边
    JQ简单图片轮播
  • 原文地址:https://www.cnblogs.com/bjwylpx/p/3680678.html
Copyright © 2020-2023  润新知