IMSE_DONT_CATCH_IMSEhtml
java.lang Class IllegalMonitorStateException java.lang.Object java.lang.Throwable java.lang.Exception java.lang.RuntimeException java.lang.IllegalMonitorStateException All Implemented Interfaces: Serializable public class IllegalMonitorStateException extends RuntimeException Thrown to indicate that a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object's monitor without owning the specified monitor.
這個問題大抵是由於給錯誤的對象使用了諸如notify(),wait()等方法,因此拋出java.lang.IllegalMonitorStateException。java
給實際上加鎖的對象進行操做就能夠了。git
例如:github
public class ThreadTest { public static void main(String[] args) { new Thread(new ThreadDemo()).start(); } } class ThreadDemo implements Runnable { private int i = 0; public synchronized void run() { try { ThreadTest.class.wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ":" + i); } }
妥妥地web
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException at java.lang.Object.wait(Native Method) at java.lang.Object.wait(Object.java:503) at com.github.alaahong.ThreadDemo.run(ThreadTest.java:14) at java.lang.Thread.run(Unknown Source)
只需將其中的api
ThreadTest.class.wait();
改爲oracle
this.wait();
便可。this