The following example expects to be able to call code in the class from the static initializer. It's not clear to me (or anyone I've asked) if this is supposed to work or not. The result if running the example below is at least that the main thread hangs.
public class CL implements Runnable {
private static final Object mutex = new Object();
private static boolean started = false;
static {
Thread t = new Thread(new CL());
t.start();
synchronized (mutex) {
try {
while (!started) {
mutex.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void run() {
started = true;
synchronized (mutex) {
mutex.notify();
}
}
public static void main(String[] args) {
System.out.println("started = " + started);
}
}
public class CL implements Runnable {
private static final Object mutex = new Object();
private static boolean started = false;
static {
Thread t = new Thread(new CL());
t.start();
synchronized (mutex) {
try {
while (!started) {
mutex.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void run() {
started = true;
synchronized (mutex) {
mutex.notify();
}
}
public static void main(String[] args) {
System.out.println("started = " + started);
}
}