/*
* Output:
Calling wait
Calling notifyAll
Done
Done
*/
class SyncFlag {
boolean flag;
synchronized void k1() {
if(flag == false) {
flag = true;
try {
System.out.println("Calling wait");
wait();
}
catch(Exception e) {
e.printStackTrace();
}
}
else {
flag = false;
System.out.println("Calling notifyAll");
notifyAll();
}
}
}
class MyThread extends Thread {
SyncFlag flag;
MyThread(SyncFlag k) {
this.flag = k;
}
public void run() {
flag.k1();
System.out.println("Done");
}
}
public class MainClass {
public static void main(String args[]) {
SyncFlag k = new SyncFlag();
new MyThread(k).start();
new MyThread(k).start();
}
}
|