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();
}
}
|
|