/*
* 单例设计模式
*
//恶汉式:
* class Single
* {
* private static final Single e= new Single();
* private Single(){}
* public static Single getInstance(){
* return s;
* }
*/
package test.itheima;
//懒汉式
class Single {
private static Single s = null;
private Single() {
}
public static Single getInstance() {
if (s == null) { // 双重判断降低判断的次数,提高效率。
synchronized (Single.class) {// 锁,该类的字节码文件对象
if (s == null) {
s = new Single();
}
}
}
return s;
}
}
public class SimpleDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
[黑马程序员]多线程_懒汉模式
最新推荐文章于 2021-05-06 20:16:10 发布