我在运行以下程序时遇到了IllegalMonitorStateException,查了一下API文档,好像说当前线程不是monitor的所有者,可是还是没搞明白也就没办法把代码改正,请各位大大帮忙看看:
public class Rendezvous{
int counter;
public synchronized void hurryUpAndWait(){
counter++;
try{
wait();
}
catch(InterruptedException ie){}
}
}
///////////////////////////////////
public class Waiter extends Thread{
public Waiter(){}
public Waiter(Rendezvous r,int id){
rendezvous = r;
this.id = id;
} public void run(){
rendezvous.hurryUpAndWait();
System.out.println("Thread "+id+":Notification has happened!");
}
Rendezvous rendezvous;
int id;
}
////////////////////////////////////////////////
public class Example5{
public static void main(String[] args){
Rendezvous rende = new Rendezvous(); int num = Integer.parseInt(args[0]);
for(int i = 0;i < num;i++){
(new Waiter(rende,i)).start();
} rende.notify(); //就这句抛出了异常
}
}
public class Rendezvous{
int counter;
public synchronized void hurryUpAndWait(){
counter++;
try{
wait();
}
catch(InterruptedException ie){}
}
}
///////////////////////////////////
public class Waiter extends Thread{
public Waiter(){}
public Waiter(Rendezvous r,int id){
rendezvous = r;
this.id = id;
} public void run(){
rendezvous.hurryUpAndWait();
System.out.println("Thread "+id+":Notification has happened!");
}
Rendezvous rendezvous;
int id;
}
////////////////////////////////////////////////
public class Example5{
public static void main(String[] args){
Rendezvous rende = new Rendezvous(); int num = Integer.parseInt(args[0]);
for(int i = 0;i < num;i++){
(new Waiter(rende,i)).start();
} rende.notify(); //就这句抛出了异常
}
}
解决方案 »
- "AWT-EventQueue-2" 错误
- Java 分割文本文件
- 异常处理的问题,请大家帮我解决,谢谢!
- 想写个计算器!!
- 问一个Java数据结构中的堆栈问题
- 如何控制HttpUrlConnection的超时时间
- sdk1.4.1为什么没有JAVAC而有一个JAVAW是什么意思?
- cloneable接口的问题!大家帮个忙啊!
- 死活找不出错的一个错误!请大虾们帮们找找原因!谢了先!!!
- tomcat 启动时出现端口被使用情况,但是项目可以跑起来,错误大概就是这样的,有些没贴如下
- 问个十分愚蠢的问题,浏览器向服务端发送数据,是不是只有表单和url这两种?
- 靠,一个帖子不能回复30贴,只好重新开贴,line0301请来这儿讨论
synchronized(rende){
rende.notify();
}