诡异的java.lang.IllegalMonitorStateException

来源:互联网 发布:网络连接服务器失败 编辑:程序博客网 时间:2024/06/05 11:43

今天的一段代码抛出了java.lang.IllegalMonitorStateException,代码如下:

[java] view plain copy
  1. private boolean wait = false;  
  2.   
  3. public boolean pleaseWait() {  
  4. synchronized (this.wait) {  
  5.     if (this.wait == true) {  
  6.         return false;  
  7.     }  
  8.   
  9.     this.wait =true;  
  10.       
  11.     try {  
  12.         this.wait.wait();  
  13.     } catch (InterruptedException e) {  
  14.   
  15.     }  
  16.   
  17.     return true;  
  18. }  

JavaDoc上关于IllegalMonitorStateException的解释是:

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.

看起来有些晦涩难懂,比如object's monitor。其实,在Object.notify()这个函数的JavaDoc中有相关的解释:

A thread becomes the owner of the object's monitor in one of three ways:
1. By executing a synchronized instance method of that object.
2. By executing the body of a synchronized statement that synchronizes on the object.
3. For objects of type Class, by executing a synchronized static method of that class. 

说白了,就是需要在调用wait()或者notify()之前,必须使用synchronized语义绑定住被wait/notify的对象。

可问题是,在上面的代码中,已经对this.wait这个变量使用了synchronzied,然后才调用的this.wait.wait()。按理不应该抛出这个异常。

上网查了很久,终于找到了答案:

真正的问题在于this.wait这个变量是一个Boolean,并且,在调用this.wait.wait()之前,this.wait执行了一次赋值操作:

[java] view plain copy
  1. this.wait = true;  
Boolean型变量在执行赋值语句的时候,其实是创建了一个新的对象。简单的说,在赋值语句的之前和之后,this.wait并不是同一个对象。

synchronzied(this.wait)绑定的是旧的Boolean对象,而this.wait.wait()使用的是新的Boolean对象。由于新的Boolean对象并没有使用synchronzied进行同步,所以系统抛出了IllegalMonitorStateException异常。

相同的悲剧还有可能出现在this.wait是Integer或者String类型的时候。

一个解决方案是采用java.util.concurrent.atomic中对应的类型,比如这里就应该是AtomicBoolean。采用AtomicBoolean类型,可以保证对它的修改不会产生新的对象。

正确的代码:

[java] view plain copy
  1. private AtomicBoolean wait = new AtomicBoolean(false);  
  2.   
  3. public boolean pleaseWait() {  
  4.     synchronized (this.wait) {  
  5.         if (this.wait.get() == true) {  
  6.             return false;  
  7.         }  
  8.   
  9.         this.wait.set(true);  
  10.   
  11.         try {  
  12.             this.wait.wait();  
  13.         } catch (InterruptedException e) {  
  14.   
  15.         }  
  16.   
  17.         return true;  
  18.     }  
  19. }  


http://blog.csdn.net/intlgj/article/details/6245226