package cn.itcast.thread;
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 SingleTest implements Runnable {
public void run() {
for (int x = 0; x < 30; x++) {
Single s = Single.getInstance();
System.out.println(s);
}
}
}
public class ThreadDemo4 {
public static void main(String[] args) {
SingleTest s = new SingleTest();
new Thread(s).start();
new Thread(s).start();
new Thread(s).start();
new Thread(s).start();
}
}
|
|