• 单例模式


    一.懒汉模式

     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 }

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

  • 相关阅读:
    Android studio 搭建测试环境 创建虚拟机
    Halcon算子翻译——break
    Halcon算子翻译——throw
    halcon算子翻译——stop
    halcon算子翻译——return
    halcon算子翻译——par_join
    Halcon算子翻译——import
    Halcon算子翻译——global
    Halcon算子翻译——export_def
    Halcon算子翻译——exit
  • 原文地址:https://www.cnblogs.com/fenglanglang/p/5995581.html
Copyright © 2020-2023  润新知