Today, I tried to wirte some code to test thread.
Use a Schedular to controll thread's running.
For thread:
public class ControllerThread extends Thread {
public void run() {
...
Schedular.waitForOthers(this);
...
}
}
In Schedular
public class Schedular {
public static synchronized void waitForOthers(ControllerThread thread) throws InterruptedException {
waitingThreadNum++; // waitingThreadNum is a counter.
if (waitingThreadNum != threads.size()) {
System.out.println("Thread:" + thread.getName() + " is going to wait");
wait(); // thread.wait() is useless here
} else {
waitingThreadNum = 0;
System.out.println("All threads will run");
notifyAll();
}
}
}
Is there anybody know why thread.wait cannot work in Schedular?