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

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

修正前

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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 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 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();
    }
}

修正後

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public <span style="color: #ff0000;">synchronized</span> 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 <span style="color: #ff0000;">synchronized</span> 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