zoukankan      html  css  js  c++  java
  • 单例模式

    一.懒汉模式

     1 /**
     2  * 懒汉模式:非线程安全的
     3  * 所谓懒汉即用到的时候才初始化
     4  */
     5 public class SingleTon {
     6     //实例成员私有化,防止被引用;设置为null,延迟加载
     7     private static SingleTon singleTon = null;
     8     //构造器私有化,只有通过getInstance来获取实例
     9     public static SingleTon getInstance(){
    10         if(singleTon == null){
    11             singleTon = new SingleTon();
    12         }
    13         return singleTon;
    14     }
    15     //构造器私有化,防止被实例化
    16     private SingleTon(){
    17         
    18     }
    19     
    20     public static void main(String[] args) {
    21         SingleTon singleTon1 = SingleTon.getInstance();
    22         SingleTon singleTon2 = SingleTon.getInstance();
    23         System.out.println(singleTon1==singleTon2);//true
    24     }
    25 }

    二.饿汉模式

     1 /**
     2  * 饿汉模式:线程安全
     3  */
     4 public class SingleTon {
     5     private static final SingleTon singleTon = new SingleTon();
     6     public static SingleTon getInstance(){
     7         return singleTon;
     8     }
     9     private SingleTon(){
    10         
    11     }
    12     public static void main(String[] args) {
    13         SingleTon singleTon1 = SingleTon.getInstance();
    14         SingleTon singleTon2 = SingleTon.getInstance();
    15         System.out.println(singleTon1==singleTon2);//true
    16     }
    17 }

    三.双重锁

     1 /**
     2  * 双重锁:懒汉模式的同步优化,线程安全
     3  */
     4 public class SingleTon {
     5     private static SingleTon singleTon = null;
     6     public static SingleTon getInstance(){
     7         if(singleTon == null){
     8             synchronized (SingleTon.class) {
     9                 if(singleTon==null){
    10                     singleTon = new SingleTon();
    11                 }
    12             }
    13         }
    14         return singleTon;
    15     }
    16     private SingleTon(){
    17         
    18     }
    19     public static void main(String[] args) {
    20         SingleTon singleTon1 = SingleTon.getInstance();
    21         SingleTon singleTon2 = SingleTon.getInstance();
    22         System.out.println(singleTon1==singleTon2);//true
    23     }
    24 }

    四.静态内部类

     1 /**
     2  * 静态内部类:线程安全
     3  */
     4 public class SingleTon {
     5     private static class SingleTonHolder{
     6         private static SingleTon singleTon = new SingleTon();
     7     }
     8     public static SingleTon getInstance(){
     9         return SingleTonHolder.singleTon;
    10     }
    11     private SingleTon(){
    12     }
    13     public static void main(String[] args) {
    14         SingleTon singleTon1 = SingleTon.getInstance();
    15         SingleTon singleTon2 = SingleTon.getInstance();
    16         System.out.println(singleTon1==singleTon2);//true
    17     }
    18 }

    总结:单线程模式下用第一种就行,多线程下最好用第二种或第四种。因为网上很多人说第二种也没有做到线程安全。

  • 相关阅读:
    Typora Writings
    Xcode7.3 beta 新功能
    最美应用API接口分析
    'Project Name' was compiled with optimization
    web前端开发与iOS终端开发的异同[转]
    2015-12-19_16_30_15
    Xcode搭建Python编译环境
    jsPach.qq.com
    Q&AApple’s Craig Federighi talks open source Swift, Objective-C and the next 20 years of development
    .NET Core项目与传统vs项目的细微不同
  • 原文地址:https://www.cnblogs.com/fenglanglang/p/5995581.html
Copyright © 2011-2022 走看看