zoukankan      html  css  js  c++  java
  • 单例

    恶汉模式
    public class hungrysingleton implements Serializable{
    private final static hungrysingleton h;
    static {
    h=new hungrysingleton();
    }
    private hungrysingleton(){
    if(h!=null){
    throw new RuntimeException("单利模式禁止反射");
    }

    }
    public static hungrysingleton getinstance(){
    return h;
    }
    private Object readResolve(){
    return h;
    }

    }

    懒汉模式
    public class Lazysingle {
    private static Lazysingle lazysingle=null;

    private Lazysingle() {
    }

    public static Lazysingle getInstance(){
    if(lazysingle==null){
    synchronized (Lazysingle.class){
    if(lazysingle==null){
    lazysingle=new Lazysingle();
    }
    }

    }
    return lazysingle;
    }
    }

    public class Lazysingle2 {
    private volatile static Lazysingle2 lazysingle=null;

    private Lazysingle2() {
    }

    public static Lazysingle2 getInstance(){
    if(lazysingle==null){
    synchronized (Lazysingle2.class){
    if(lazysingle==null){
    lazysingle=new Lazysingle2();
    }
    }

    }
    return lazysingle;
    }
    }
  • 相关阅读:
    poj 1562 Oil Deposits
    poj 1650 Integer Approximation
    snmp4j 编程
    ubuntu 13.04 163源(亲测可用)
    c语言中static 用法总结(转)
    Spring入门
    Hibernate入门
    Struts2入门教程
    素数距离问题
    ASCII码排序
  • 原文地址:https://www.cnblogs.com/qinyios/p/11062142.html
Copyright © 2011-2022 走看看