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

    参考链接:https://www.cnblogs.com/Ycheng/p/7169381.html

    1、懒汉--线程不安全模式

    public class SingletanceDemo01{
        private static SingletanceDemo01 instance;
              
         private SingletanceDemo01(){}
    
         public static SingletanceDemo01 getInstance(){
              if(instance == null){
                     instance = new SingletanceDemo01();
              }
              return instance;
         }
    }

    2、懒汉--线程安全

    public class SingletanceInstance{
        private static SingletanceInstance instance;
        private SingletanceInstance(){}
        public static synchronized SingletanceInstance getInstance(){
              if(instance == null){
                     instance = new SingletanceInstance();
              }
              return instance;
        }  
    
    }    

    3、饿汉

    public class SingletonDemo{
        private static SingletonDemo instance = new SingletonDemo();
        private SingletonDemo(){}
        public static SingletonDemo getInstance(){
               return instance;
        }  
    
    }
  • 相关阅读:
    FreePascal
    Delphi
    FreePascal
    FreePascal
    Linux
    FreePascal
    FreePascal
    CodeTyphon
    IDEA
    工作流科普——don't ask i don't know either
  • 原文地址:https://www.cnblogs.com/yingpu/p/9446721.html
Copyright © 2011-2022 走看看