您的位置:首页 > 职场人生

[黑马程序员]多线程_懒汉模式

2014-03-16 14:46 369 查看
/*
* 单例设计模式
*
//恶汉式:
* 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

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: