class Single {
private Single() {
}
private static Single s = null;
public static Single get() {
if (s == null) { //提高效率,不让线程每次都运行同步。
synchronized (Demo1.class) {
if (s == null) {
s=new Single();
}
}
}
return s;
}
}
class Show implements Runnable {
public void run() {
Single s = Single.get();
System.out.println(s);
}
}
class Demo1 {
public static void main(String[] args) {
Show s = new Show();
Thread t = new Thread(s);
Thread t2 = new Thread(s);
t.start();
t2.start();
}
}