ITeye博客

多线程-单例设计模式-懒汉式

wststar · 2012-05-22 08:57 · JAVA基础_多线程 · 编程语言 · Markdown

/**

多线程-单例设计模式-懒汉式

单例设计模式-饿汉式

class Single

{

private static final Single s = new Single();

private Single(){}

public static getInstance(){

return s;

}

} 

单例设计模式-懒汉式 延迟加载 解决多线程安全问题 

用同步函数效率慢 使用双重判断 同步代码块判断 效率快

*/

class Single

{

private static Single s = null;

private Single(){}

public static getInstance(){

if(null == s){

synchronized(Single.class){

if(null == s){

s = new Single();

}

return s;

}

}

}

}

class SingleDemo 

{

public static void main(String[] args) 

{

System.out.println("Hello World!");

}

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。