Usage example:
public class
Test extends Thread {
public static class LockThread extends Thread {
Object lock;
intid;
public LockThread (int id, Object lock) {
this.lock = lock;
this.id = id;
}
public void run() {
System.out.println (id+" I will wait notification!");
synchronized (lock) {
try {
lock.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println (id+" I receive notification!");
}
}
public static void main(
String strs[]) throws Exception {
Object lock1 = new Object();
Object lock2 = new Object();
LockThread t1 = new LockThread(1, lock1);
LockThread t2 = new LockThread(2, lock2);
LockThread t3 = new LockThread(3, lock2);
t1.start();
t2.start();
t3.start();
Thread.currentThread().sleep(200);
synchronized (lock2) {
lock2.notifyAll();
}
synchronized (lock1) {
lock1.notify();
}
}
}