public class TestWaitNotifyMutex {
boolean isF1 = true;
public static void main(String[] args) {
final TestWaitNotifyMutex twnm = new TestWaitNotifyMutex();
new Thread(){
public void run() {
twnm.f1();
};
}.start();
new Thread(){
public void run() {
twnm.f2();
};
}.start();
}
private synchronized void f1(){
try {
while(isF1){
System.out.println("111111");
isF1 = false;
notifyAll();
wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private synchronized void f2(){
try {
while(!isF1){
System.out.println("222222");
isF1 = true;
notifyAll();
wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
boolean isF1 = true;
public static void main(String[] args) {
final TestWaitNotifyMutex twnm = new TestWaitNotifyMutex();
new Thread(){
public void run() {
twnm.f1();
};
}.start();
new Thread(){
public void run() {
twnm.f2();
};
}.start();
}
private synchronized void f1(){
try {
while(isF1){
System.out.println("111111");
isF1 = false;
notifyAll();
wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private synchronized void f2(){
try {
while(!isF1){
System.out.println("222222");
isF1 = true;
notifyAll();
wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}