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;

             }

    }

     

  • 相关阅读:
    Gerrit 系统初探 (已转移到 https://steemit.com/gerrit/@linvictor88/gerrit )
    Iaas概述
    题解西电OJ (Problem 1007 -做一名正气的西电人 )--长整型计算
    题解西电OJ (Problem 1005 -跳舞毯)--动态规划
    题解西电OJ (Problem 1004 -亚特兰提斯)--最小生成树
    题解西电OJ (Problem 1003 -最喜欢的数字)--动态规划
    题解西电OJ (Problem 1008
    题解西电OJ (Problem 1006
    HTML-css selector
    Android--应用开发3(Android layout XML属性)
  • 原文地址:https://www.cnblogs.com/gxpblogs/p/3068001.html
Copyright © 2011-2022 走看看