简体   繁体   English

同步块中的IllegalMonitorStateException

[英]IllegalMonitorStateException inside synchronized block

Though I have written wait inside synchronized block. 虽然我已经在同步块中写了等待。 I am getting IllegalMonitorStateException . 我正在获取IllegalMonitorStateException What's the reason then? 那是什么原因

package trials;

public class WaitNotifyTrial {

    public static void main(String[] args){
        Generator g=new Generator();
        g.start();
        System.out.println("Start");
        synchronized (g) {
                try {
                    g.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    System.out.println("Printing exception");
                    e.printStackTrace();
                }
                System.out.println(g.value);
        }
    }
}

class Generator extends Thread{

    int value;

    public void run(){
        synchronized (this) {
            value=10;
        }
        notify();
        }
    }
}

These are some of the things wrong with your code: 这些是您的代码有问题的一些地方:

  • you call notify() outside of synchronized (this) (this is your immediate problem); 您在synchronized (this) notify()之外调用notify() (这是您的直接问题);
  • you don't use the proper wait-loop idiom, thus risking nondeterministic behavior/deadlocks in the face of spurious wakeups and missed notifications ; 您没有使用适当的等待循环习惯用法,因此在虚假唤醒错过通知的情况下冒着不确定性的行为/死锁的风险;
  • you use the wait-notify mechanism on a Thread instance, which is recommended against in its documentation ; 您可以在Thread实例上使用wait-notify机制, 建议在其文档中使用
  • you extend Thread instead of using that class as-is and only passing it an instance of your implementation of Runnable . 您可以扩展Thread而不是按原样使用该类,而仅将Runnable实现的一个实例传递给它。

For almost a whole decade now, the general advice has been to avoid the wait-notify mechanism altogether and instead use one of synchronization aids from java.util.concurrent , such as the CountDownLatch . 在将近整整十年的时间里,一般的建议是完全避免使用wait-notify机制,而应使用java.util.concurrent一种同步辅助工具,例如CountDownLatch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IllegalMonitorStateException在其run方法内部的线程上调用wait()(无同步块) - IllegalMonitorStateException calling wait() on a thread inside its run method (with no synchronized block) 什么可以导致同步块内的IllegalMonitorStateException? - What can cause IllegalMonitorStateException from inside a synchronized block? 同步块内的同步块 - Synchronized block inside synchronized block 为什么在同步块外部调用notifyAll()时引发IllegalMonitorStateException? - Why the IllegalMonitorStateException is raised upon notifyAll() call outside a synchronized block? 从同步块中调用wait时发生java.lang.IllegalMonitorStateException - java.lang.IllegalMonitorStateException whilst calling wait from synchronized block @Synchronized 方法中的 Kotlin IllegalMonitorStateException - Kotlin IllegalMonitorStateException in @Synchronized method Java 同步:IllegalMonitorStateException - Java synchronized : IllegalMonitorStateException Java synchronized String IllegalMonitorStateException - Java synchronized String IllegalMonitorStateException 当我以静态方式同步块调用wait()时,为什么Java会抛出java.lang.IllegalMonitorStateException? - Why Java throw java.lang.IllegalMonitorStateException when I invoke wait() in static way synchronized block? 同步块内部运行方法 - Synchronized Block inside run method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM