private static volatile Boolean aBoolean = true;
static class A implements Runnable{
@Override
public void run() {
synchronized (aBoolean){
while (aBoolean){
try {
System.out.println("进入A, 等待中");
aBoolean.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("A over");
}
}
}
static class B implements Runnable{
@Override
public void run() {
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (aBoolean){
System.out.println("通知A可以运行了");
aBoolean = false;
aBoolean.notifyAll();
//Boolean.TRUE.notifyAll();
}
}
}
public static void main(String[] args) {
new Thread(new A()).start();
new Thread(new B()).start();
}