public class Test {
public static Object object = new Object();
public static boolean printA = true;
public static void main(String[] args) {
ThreadA threadA = new ThreadA();
threadA.start();
ThreadB threadB = new ThreadB();
threadB.start();
}
}
class ThreadA extends Thread {
@Override
public void run() {
for(int i = 0; i < 10; i++) {
synchronized (Test.object) {
if(!Test.printA) {
try {
Test.object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
System.out.println("A");
Test.printA = false;
Test.object.notify();
}
}
}
}
}
class ThreadB extends Thread {
@Override
public void run() {
for(int i = 0; i < 10; i++) {
synchronized (Test.object) {
if(Test.printA) {
try {
Test.object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
System.out.println("B");
Test.printA = true;
Test.object.notify();
}
}
}
}
}
运行以上代码,为什么会一直不结束无限wait下去?
慕码人2483693
吃鸡游戏
慕侠2389804
慕村9548890
相关分类