DESKTOP-D9IGLU5_20200903-赵继豪

1、问题

1、饿汉式单例(是线程安全(thread-safe)的)

​ 在程序启动或单例模式类被加载的时候,单例模式实例就已经被创建

public final class Earth {
    
    private static final Earth EARTH = new Earth();
    
    private Earth() {
        super();
    }
    public static Earth getInstance() {
        return EARTH ;
    }

}

2、懒汉式单例(不是线程不安全(thread-safe)的)

​ 只有当调用getInstance的时候,才初始化这个单例。

  ```java

public final class Moon {

private static Moon single ;

private Moon() {
    super();
}
public static Moon getInstance() {
    if( single == null ) { 
        single = new Moon();
    }
    return single ;
}

}
```

2、吐槽

前阵子嘴里烂了的口子好不容易好了,最近又烂开了,服了

标签


© 2021 成都云创动力科技有限公司 蜀ICP备20006351号-1