诡异的java.lang.IllegalMonitorStateException
来源:互联网 发布:网络连接服务器失败 编辑:程序博客网 时间:2024/06/05 11:43
今天的一段代码抛出了java.lang.IllegalMonitorStateException,代码如下:
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执行了一次赋值操作:
Boolean型变量在执行赋值语句的时候,其实是创建了一个新的对象。简单的说,在赋值语句的之前和之后,this.wait并不是同一个对象。synchronzied(this.wait)绑定的是旧的Boolean对象,而this.wait.wait()使用的是新的Boolean对象。由于新的Boolean对象并没有使用synchronzied进行同步,所以系统抛出了IllegalMonitorStateException异常。
相同的悲剧还有可能出现在this.wait是Integer或者String类型的时候。
一个解决方案是采用java.util.concurrent.atomic中对应的类型,比如这里就应该是AtomicBoolean。采用AtomicBoolean类型,可以保证对它的修改不会产生新的对象。
正确的代码:
http://blog.csdn.net/intlgj/article/details/6245226
- 诡异的java.lang.IllegalMonitorStateException
- 诡异的java.lang.IllegalMonitorStateException
- 诡异的java.lang.IllegalMonitorStateException异常
- java.lang.IllegalMonitorStateException错误的解决
- java.lang.IllegalMonitorStateException翻译
- java.lang.IllegalMonitorStateException
- java.lang.IllegalMonitorStateException
- java.lang.IllegalMonitorStateException
- java.lang.IllegalMonitorStateException
- java.lang.IllegalMonitorStateException
- 处理java.lang.IllegalMonitorStateException
- java.lang.IllegalMonitorStateException
- java.lang.IllegalMonitorStateException
- java.lang.IllegalMonitorStateException异常
- 关于java.lang.IllegalMonitorStateException
- 关于java.lang.IllegalMonitorStateException
- 关于java.lang.IllegalMonitorStateException
- 关于java.lang.IllegalMonitorStateException
- Spring Security(09)——Filter介绍
- 如何使用WinZip给图片加水印?
- 金蝶EAS,代码创建临时表,参考代码
- Debian系统中同步系统的时间
- 三大运营商无线频谱的大体分布-自制表格版
- 诡异的java.lang.IllegalMonitorStateException
- 无框架加载table表中数据
- 玩转Google开源C++单元测试框架Google Test系列(gtest)之三
- Java_log2000_类与对象7
- CentOS下yum的安装及配置
- MySQL日期时间函数大全
- 用servlet获取IP等信息
- angular2 测试问题
- 为Cubieboard2构建自己的的Debian Linux的步骤(3)(分色排版)下载并配置debian系统