java.lang.IllegalMonitorStateException

java.lang.IllegalMonitorStateException
违法的监控状态异常。当某个线程试图等待一个自己并不拥有的对象(O)的监控器或者通知其他线程等待该对象(O)的监控器时,抛出该异常。

例子:

//计算线程

1. package com.intlgj.thread;
2. //计算线程
3. public class Calculator extends Thread {
4. int total;
5. public void run() {
6. synchronized (this) {
7. for (int i = 0; i < 10; i++) {
8. total += i;
9. }
10. }
11. // 通知所有在此对象上等待的线程
12. notifyAll();
13. }
14. }

package com.intlgj.thread; //计算线程 public class Calculator extends Thread { int total; public void run() { synchronized (this) { for (int i = 0; i < 10; i++) { total += i; } } // 通知所有在此对象上等待的线程 notifyAll(); } }

//获取计算结果并输出

view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

1. package com.intlgj.thread;
2. //获取计算结果并输出
3. public class ReaderResult extends Thread {
4. Calculator c;
5. public ReaderResult(Calculator c) {
6. this.c = c;
7. }
8. public void run() {
9. synchronized (c) {
10. try {
11. System.out.println(Thread.currentThread() + "等待计算结果。。。");
12. c.wait();
13. } catch (InterruptedException e) {
14. e.printStackTrace();
15. }
16. System.out.println(Thread.currentThread() + "计算结果为:" + c.total);
17. }
18. }
19. public static void main(String[] args) {
20. Calculator calculator = new Calculator();
21. // 启动10个线程,分别获取计算结果
22. for(int i=0;i<5;i++){
23. new ReaderResult(calculator).start();
24. }
25. // 启动计算线程
26. calculator.start();
27. }
28. }

package com.intlgj.thread; //获取计算结果并输出 public class ReaderResult extends Thread { Calculator c; public ReaderResult(Calculator c) { this.c = c; } public void run() { synchronized (c) { try { System.out.println(Thread.currentThread() + "等待计算结果。。。"); c.wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread() + "计算结果为:" + c.total); } } public static void main(String[] args) { Calculator calculator = new Calculator(); // 启动10个线程,分别获取计算结果 for(int i=0;i<5;i++){ new ReaderResult(calculator).start(); } // 启动计算线程 calculator.start(); } }

运行结果

Thread[Thread-1,5,main]等待计算结果。。。

Thread[Thread-2,5,main]等待计算结果。。。

Thread[Thread-3,5,main]等待计算结果。。。

Thread[Thread-4,5,main]等待计算结果。。。

Thread[Thread-5,5,main]等待计算结果。。。

Thread[Thread-5,5,main]计算结果为:45

Thread[Thread-4,5,main]计算结果为:45

Thread[Thread-3,5,main]计算结果为:45

Thread[Thread-2,5,main]计算结果为:45

Thread[Thread-1,5,main]计算结果为:45

Exception in thread "Thread-0" java.lang.IllegalMonitorStateException

at java.lang.Object.notifyAll(Native Method)

at com.intlgj.thread.Calculator.run(Calculator.java:15)

根据SCJP所要求的线程交互知识点需要从java.lang.Object的类的三个方法和以上的说法:

void notify()
唤醒在此对象监视器上等待的单个线程。
void notifyAll()
唤醒在此对象监视器上等待的所有线程。
void wait()
导致当前的线程等待,直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法。
根据jdk的void notifyAll()的描述,“解除那些在该对象上调用wait()方法的线程的阻塞状态。该方法只能在同步方法或同步块内部调用。如果当前线程不是对象所得持有者,该方法抛出一个java.lang.IllegalMonitorStateException 异常”
所以我们现在就可以明确错误的原因了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值