为什么我得到线程t1的IllegalMonitorStateException



下面的代码出现此错误

First thread about to sleep
thread 1  run
Boolean assignment done.
Woke up and about to invoke wait()
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:502)
at IncorrectSynchronization$1.run(HelloWorld.java:23)
at java.lang.Thread.run(Thread.java:748)

当线程t1处于睡眠状态时,我从另一个线程将锁修改为false。然后它抛出这个IllegalMonitorStateException。它仍然是同一个对象,为什么修改值会导致IllegalMonitorStateException?

当我从同步块内的另一个线程将锁修改为false时,我不再得到那个错误。有人能解释一下引擎盖下发生的事情的原因吗?

public class HelloWorld{
public static void main( String args[] ) throws InterruptedException {
SampleTest.runExample();
}
}
class SampleTest{
Boolean flag = new Boolean(true);
public void example() throws InterruptedException {
Thread t0 = new Thread(new Runnable() {
public void run() {
synchronized (flag) {
try {
while (flag) {
System.out.println("First thread about to sleep");
Thread.sleep(2000);
System.out.println("Woke up and about to invoke wait()");
flag.wait();
System.out.println("wait() called");
}
} catch (InterruptedException ie) {
}
}
}
});
Thread t1 = new Thread(new Runnable() {
public void run() {
System.out.println("thread 1  run");
flag = false;
}
});
t0.start();
Thread.sleep(200);
t1.start();
t0.join();
t1.join();
}
public static void runExample() throws InterruptedException {
SampleTest test = new SampleTest();
test.example();
}
}

问题出在这行:

flag = false;

这将更改flag布尔变量的引用,从原始Boolean对象(由不推荐使用的构造函数创建,不应使用(更改为预先创建的Boolean.FALSE实例(由于自动装箱(。当第一个线程调用flag.wait()时,对象不再是它用来同步的对象,因此是IllegalMonitorStateException

在这种情况下,最好使用AtomicBoolean并在另一个线程中更改其值:

AtomicBoolean flag = new AtomicBoolean(true);

现在,第二个线程可以更新同一对象的值。它可能还应该通知等待对象的第一个线程(与wait()一样,notify()也需要在调用它的对象上同步(:

Thread t1 = new Thread(new Runnable() {
public void run() {
synchronized(flag) {
System.out.println("thread 1  run");
flag.set(false);
flag.notify();
}
}
});

最新更新