在Integer上同步时对notify()的IllegalMonitorStateException
我刚接触在Java中使用wait()和notify(),我得到一个IllegalMonitorStateException。
主要代码
public class ThreadTest { private static Integer state = 0; public static void main(String[] args) { synchronized(state) { System.out.println("Starting thread"); Thread t = new Thread(new AnotherTest()); t.start(); synchronized(state) { state = 0; while(state == 0) { try { state.wait(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("State is: " + state); } } } public static class AnotherTest implements Runnable { @Override public void run() { synchronized(state) { state = 1; state.notify(); } } } }
我收到一个IllegalMonitorStateException调用state.notify()。 有任何想法吗?
编辑 :根据下面的答案,这里是有效的代码。 作为旁注,我首先尝试使用枚举,这与使用整数有相同的问题。
public class ThreadTest { private static int state = 0; private static Object monitor = new Object(); public static void main(String[] args) { synchronized(monitor) { System.out.println("Starting thread"); Thread t = new Thread(new AnotherTest()); t.start(); state = 0; while(state == 0) { try { for(int i = 0; i < 5; i++) { System.out.println("Waiting " + (5 - i) + " Seconds"); Thread.sleep(1000); } monitor.wait(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("State is: " + state); } } public static class AnotherTest implements Runnable { @Override public void run() { synchronized(monitor) { state = 1; monitor.notify(); } } } }
这个
private static Integer state = 0;
相当于
private static Integer state = Integer.valueOf(0);
valueOf(0)
的调用返回对Integer
对象的引用,称之为A.
然后你做
synchronized(state) {
你的线程获取state
引用的对象的锁,目前是A.
然后你做
state = 1;
这相当于
state = Integer.valueOf(1);
它为您提供了对Integer
对象的不同引用,将其称为B,并将其分配给state
。 然后你打电话
state.notify();
你正在对一个对象B调用notify()
,你的线程不拥有该监视器。 您不能在线程不拥有监视器的对象上调用notify
或wait
。