java.lang.IllegalMonitorStateException 关于 notifyAll() 方法
java.lang.IllegalMonitorStateException on notifyAll() method
我仍在按照 oracle 网站的 java 教程学习 Threads。
关于wait()和notifyAll(),我写了一些代码。当 运行 中的 'joy' 设置为 false 时,我的预期输出是在 运行() 中打印消息 10 次并在 guardedJoy(GuardedBlock guardedBlock)
方法中打印 "Fun stopped by StopFun Thread" 消息方法。
这是我的代码。
public class GuardedBlock {
private boolean joy = true;
public synchronized void guardedJoy(GuardedBlock guardedBlock) {
System.out.println(Thread.currentThread().getName() + " Guard Joy method started");
while (guardedBlock.joy) {
try {
System.out.println(Thread.currentThread().getName() + " Going to waiting state");
guardedBlock.wait();
} catch (InterruptedException ex) {
}
}
System.out.println("Fun stopped by StopFun Thread");
}
private static class StopFun implements Runnable {
private GuardedBlock guardedBlock;
public StopFun(GuardedBlock guardedBlock) {
this.guardedBlock = guardedBlock;
}
@Override
public void run() {
for (int x = 0; x < 100; x++) {
try {
Thread.sleep(500);
System.out.println("Allowing fun since its only " + x + " times - " + Thread.currentThread().getName());
if (x == 10) {
guardedBlock.joy = false;
guardedBlock.notifyAll();
break;
}
} catch (InterruptedException ex) {
}
}
}
}
public static void main(String[] args) {
GuardedBlock guardedBlock = new GuardedBlock();
StopFun sf = new StopFun(guardedBlock);
Thread stopFun = new Thread(sf);
stopFun.start();
guardedBlock.guardedJoy(guardedBlock);
}
}
运行 方法中的代码 运行 没问题,但之后会抛出这样的异常。
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
at java.lang.Object.notifyAll(Native Method)
at Synchronization.GuardedBlock$StopFun.run(GuardedBlock.java:38)
at java.lang.Thread.run(Thread.java:748)
我在 this and this 之类的网站上经历了几个问题和答案,但无法弄清楚我到底做错了什么。帮助非常重要。
谢谢。
wait()
和 notify()
/notifyAll()
必须在 synchronized
块中调用。
synchronized (guardedBlock) {
guardedBlock.notifyAll();
}
等等。
我仍在按照 oracle 网站的 java 教程学习 Threads。
关于wait()和notifyAll(),我写了一些代码。当 运行 中的 'joy' 设置为 false 时,我的预期输出是在 运行() 中打印消息 10 次并在 guardedJoy(GuardedBlock guardedBlock)
方法中打印 "Fun stopped by StopFun Thread" 消息方法。
这是我的代码。
public class GuardedBlock {
private boolean joy = true;
public synchronized void guardedJoy(GuardedBlock guardedBlock) {
System.out.println(Thread.currentThread().getName() + " Guard Joy method started");
while (guardedBlock.joy) {
try {
System.out.println(Thread.currentThread().getName() + " Going to waiting state");
guardedBlock.wait();
} catch (InterruptedException ex) {
}
}
System.out.println("Fun stopped by StopFun Thread");
}
private static class StopFun implements Runnable {
private GuardedBlock guardedBlock;
public StopFun(GuardedBlock guardedBlock) {
this.guardedBlock = guardedBlock;
}
@Override
public void run() {
for (int x = 0; x < 100; x++) {
try {
Thread.sleep(500);
System.out.println("Allowing fun since its only " + x + " times - " + Thread.currentThread().getName());
if (x == 10) {
guardedBlock.joy = false;
guardedBlock.notifyAll();
break;
}
} catch (InterruptedException ex) {
}
}
}
}
public static void main(String[] args) {
GuardedBlock guardedBlock = new GuardedBlock();
StopFun sf = new StopFun(guardedBlock);
Thread stopFun = new Thread(sf);
stopFun.start();
guardedBlock.guardedJoy(guardedBlock);
}
}
运行 方法中的代码 运行 没问题,但之后会抛出这样的异常。
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
at java.lang.Object.notifyAll(Native Method)
at Synchronization.GuardedBlock$StopFun.run(GuardedBlock.java:38)
at java.lang.Thread.run(Thread.java:748)
我在 this and this 之类的网站上经历了几个问题和答案,但无法弄清楚我到底做错了什么。帮助非常重要。
谢谢。
wait()
和 notify()
/notifyAll()
必须在 synchronized
块中调用。
synchronized (guardedBlock) {
guardedBlock.notifyAll();
}
等等。