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

    http://baike.baidu.com/view/1859857.htm
    单例模式一般有三种形式

    //1.饿汉式

    public class Singleton1 {

     

             private static Singleton1 instance = new Singleton1();

     

             private Singleton1() {

             }

     

             static Singleton1 getInstance() {

                       return instance;

             }

     

    }


    *************************************************************************************************************************************************************************

    //2.懒汉式

    public class Singleton2 {

     

             private static Singleton2 instance = null;

     

             private Singleton2() {

             }

     

             static Singleton2 getInstance() {

                       if (instance == null)

                                instance = new Singleton2();

                       return instance;

             }

    }

    *************************************************************************************************************************************************************************


    //
    双重锁的形式。

    public class Singleton3 {

             private static Singleton3 instance = null;

             private Singleton3(){

                       //do something

             }

             public static Singleton3 getInstance(){

                       if(instance==null){

                                synchronized(Singleton3.class){

                                         if(null == instance){ 

                                                   instance = new Singleton3();

                                         }

                                }

                       }

                       return instance;

             }

    }

     

  • 相关阅读:
    解决undefined reference to `__poll_chk@GLIBC_2.16' 错误
    交叉编译总结 libosscore.a libcurl.a libmysqlclient.a
    APUE环境配置
    UDT中epoll对CLOSE状态的处理
    查看ld搜索路径
    linux shell 比较文件夹内容 diff
    交互式makefile
    linux shell取文本最后一行
    linux 查看静态库,动态库是32位还是64位
    python学习day4之路
  • 原文地址:https://www.cnblogs.com/gxpblogs/p/3068001.html
Copyright © 2011-2022 走看看