Suppose you have this class:
public class Singleton implements Runnable {
public static final String A_STRING = "Hello World".toLowerCase();
public static final Singleton INSTANCE = new Singleton();
public static Singleton getInstance() {
return INSTANCE;
}
private Singleton() {
super();
launchThread();
}
public void launchThread() {
synchronized (this) {
Thread t = new Thread(this);
t.start();
while (t.isAlive()) {
try {
wait(100);
} catch (InterruptedException e) {
// Interrupted.
}
}
}
}
public void run() {
System.out.println(A_STRING);
}
public static void main(String[] args) {
Singleton.getInstance();
}
}
If you run the main method, it hangs in a deadlock. Can you see the reason?


