「java」java.lang.IllegalMonitorStateExceptionエラーの解決方法

エラーメッセージ
Exception in thread “xxxx" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)

修正前

public void get() {
    if (!flag) {
        try {
              this.wait();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(this.count + this.type);
        flag = false;
        this.notify();
    }
}

修正後

public synchronized void get() {
        if (!flag) {
            try {
                  this.wait();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(this.count + this.type);
            flag = false;
            this.notify();
        }
}

 

Java

Posted by arkgame