有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么线程t1会出现非法监视器状态异常

我在下面的代码中得到了这个错误

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

当我从同步块中的另一个线程将锁修改为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();
    }
}

共 (1) 个答案

  1. # 1 楼答案

    问题在于这一行:

    flag = false;
    

    这会将flag布尔变量的引用从原始Boolean对象(由不推荐使用的构造函数创建)更改为预创建的Boolean.FALSE实例(由于autoboxing)。当第一个线程调用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();
             }
           }
     });