读者写者问题的模拟出现java.lang.IllegalMonitorStateException异常

chenjie19891104 2009-10-06 08:28:02
下面是对读者写者问题的模拟,但是运行时,总是出现java.lang.IllegalMonitorStateException异常。检查是notify和wait两个语句的问题。虽然将wait和notify都放在synchronized(){}中,可以消除异常,但是程序无法正常终止,似乎是出现了死锁。但是将wait不放在synchronized(){}中,虽然程序可以运行完成,但会出现上述异常。部分代码如下:

class Writer extends Thread{
private Resource resource;
private int id; //读者进程id

public Writer(Resource resource, int id){
this.id = id;
this.resource = resource;
}

public void run(){

try{
resource.decreWmutex();
if(resource.getWmutex() < 0){ //如果wmutex已被某writer或reader进程占据,等待
System.out.println("写进程" + this.id + "被阻塞!");

wait();
}else{
System.out.println("写进程" + this.id + "正在操作!");
Thread.sleep(300);//正在访问时间
}

}catch(Exception e){
e.printStackTrace();
}finally{
resource.increWmutex();
System.out.println("写进程" + this.id + "操作完毕!");
synchronized(this){
notifyAll();
} //notifyAll();

}

}
}

class Reader extends Thread{

private Resource resource;
private int id;

public Reader(Resource resource, int id){
this.resource = resource;
this.id = id;
}

public void run(){
try{
resource.decreRmutex();
if(resource.getRmutex() < 0){
System.out.println("读进程" + this.id + "被阻塞!未进入");

wait();

}else{
if(resource.getReadercount() == 0){//如果是0,判断有没有写进程在里面。
resource.decreWmutex();
if(resource.getWmutex() < 0){
System.out.println("读进程" + this.id + "被阻塞!");

wait();
}
}
resource.increReadercount(); //增加一个reader进程
resource.increRmutex();//释放rmutex;
System.out.println("读进程" + this.id + "进入!" + "当前进程数是" + resource.getReadercount());
Thread.sleep(100);//模仿正在访问时间

resource.decreRmutex();//重新访问rmutex
if(resource.getRmutex() < 0){
System.out.println("读进程" + this.id + "被阻塞!未出来");

wait();
}else{
resource.decreReadercount();
if(resource.getReadercount() == 0) //如果,没有reader进程在里面,唤醒writer进程
resource.increWmutex();
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
resource.increRmutex();//释放rmutex
System.out.println("读进程" + this.id + "操作完毕!" + "当前进程数是" + resource.getReadercount());

//notifyAll();
synchronized(this){
notifyAll();
}
}
}
}

public class RWProblem {
public static void main(String args[]){
Resource resource = new Resource();

//定义2个写者进程
for(int i = 0; i < 2; i++){
Thread t = new Writer(resource, i);
t.start();
}
//定义5个读者进程
for(int i = 0; i < 5; i++){
Thread t = new Reader(resource, i);
t.start();
}
}

}


请各位帮我看看》》》
...全文
701 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenjie19891104 2009-10-07
  • 打赏
  • 举报
回复
对了,有时间请大家帮我看看我的那个读者写者的问题。
http://topic.csdn.net/u/20091006/20/e9985d3b-4dea-4293-ad04-977e9cbf86cf.html
chenjie19891104 2009-10-07
  • 打赏
  • 举报
回复
可是现在要模拟的是多个读者是可以同时对共享资源(文件)进行操作啊,只是readercount对于他们来说是临界的,所以,哎,我也不知道怎么说。看来还是对整个问题的理解不透彻。我好好想想。
平淡面对 2009-10-07
  • 打赏
  • 举报
回复
这个程序有很多问题。只想指出一点,在判断Resouce的状态时,必须保证只有一个线程在读,没有其他线程在改变Resouce的状态。所以这部分的代码都要用到synchronized。
zl3450341 2009-10-07
  • 打赏
  • 举报
回复

public class IllegalMonitorStateException extends RuntimeException
zl3450341 2009-10-07
  • 打赏
  • 举报
回复

public class IllegalMonitorStateExceptionextends RuntimeException
抛出的异常表明某一线程已经试图等待对象的监视器,或者试图通知其他正在等待对象的监视器而本身没有指定监视器的线程。



api里面写的

你自己对着找一下看看
ddrr2009 2009-10-07
  • 打赏
  • 举报
回复
顶~~~不过要留个连接,嘿嘿~~~可能你会有兴趣哦http://bbs.tsp2c.cn/?fromuid=136
chenjie19891104 2009-10-06
  • 打赏
  • 举报
回复
对了,共享资源类定义如下:
//定义资源类
class Resource{
private int wmutex;
private int rmutex;
private int readercount;

public Resource(){
wmutex = 1; //初始值为1,该资源信号量对reader进程和writer进程而言
rmutex = 1; //初始值为1,该资源信号量对所有reader进程而言。
readercount = 0; //记录访问文件的reader进程数
}

public synchronized void increWmutex(){
this.wmutex++;

}

public synchronized void decreWmutex(){
this.wmutex--;

}

public synchronized void increRmutex(){
this.rmutex++;

}

public synchronized void decreRmutex(){
this.rmutex--;

}

public synchronized void increReadercount(){
this.readercount++;
}

public synchronized void decreReadercount(){
this.readercount--;
}

public int getWmutex(){
return this.wmutex;
}

public int getRmutex(){
return this.rmutex;
}

public int getReadercount(){
return this.readercount;
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧