标题: 单例模式懒汉 [打印本页] 作者: 七色★碎羽 时间: 2015-9-14 22:41 标题: 单例模式懒汉 public class Single {
private Single(){}
private static Single s = null;
public static Single getInstance(){
if(s == null){
synchronized(Single.class){
if( s == null )
s = new Single();
}
}
return s;
}
}
class SingleRunnable implements Runnable{
public void run(){
for(int x = 0 ; x < 10 ;x++){
Single s = Single.getInstance();
System.out.println(s);
}
}
}
public class SingleTest {
public static void main(String[] args) {
SingleRunnable s = new SingleRunnable();
new Thread(s).start();
new Thread(s).start();
}
}