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

    下面是单例设计模式的两种设计方式

    饿汉式:使用的时候多用,同步的,可以保证唯一性

    public class Singleton {//饿汉式

        private static Singleton s = new Singleton();

        private Singleton(){}   

        public static Singleton getInstance(){

           return s;

        }

    }

    懒汉式:考试的时候多用,非同步的,不能保证唯一性,涉及同步问题,考点较多

    public class Singleton{//低效率懒汉式

        private static Singleton s = null;

        private Singleton(){}   

        public static synchronized Singleton getInstance(){

           if(s==null)

               s = new Singleton();

           return s;

        }

    }

     

    public class Singleton{//高效率懒汉式

        private static Singleton s = null;

        private Singleton(){}   

        public static Singleton getInstance(){

           if(s==null){

               synchronized(Singleton.class){

                   if(s==null)

                      s = new Singleton();

               }         

           }

           return s;        

        }

    }

  • 相关阅读:
    2019年8月下旬
    2019年8月上旬
    2019年7月 vue专题
    2019年7月上
    mysql安装 demo [linux centos7] [5.7.26]
    记一个日志冲突——管中窥豹[java混乱的日志体系]
    Mybatis-Generator demo
    dubbo doc入门文档
    springBoot+mysql+mybatis demo [基本配置] [遇到的问题]
    nginx安装demo
  • 原文地址:https://www.cnblogs.com/talkice/p/3352804.html
Copyright © 2011-2022 走看看