• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why got java.lang.IllegalMonitorStateException

 
Harry Lufei
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In client side, there are code:
try{
remoteserver.lock(recordnumber[k]);
rv=remoteserver.bookSeats(recordnumber[k],seats);
remoteSvr.unlock(recordnumber[k]);
}catch (Exception e){
System.out.println(e);
}
why always got java.lang.IllegalMonitorStateException when running even only one client connected with remote server object by rmi
in server side there are code:
public void lock(int record) throws IOException,InterruptedException {
synchronized(hashmap){
if(hashmap.contains( mylock )) wait();
else lockedrec.add( mylock );
}
}
Any explaination will be very appreciated
 
morph wang
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Harry Lufei:

synchronized(hashmap){
if(hashmap.contains( mylock ))
wait();
else
lockedrec.add( mylock );
}
Any explaination will be very appreciated

Seems you didn't totally understand the synchronization mechanism, the wait()/notify()/notifyall() method are instance methods of an object on which the lock is being holding by the caller. In above code, you are holding the lock of 'hashmap', so in this sync block, you can only invoke hashmap.wait() rather than wait()(which is identical with this.wait(), this is the current object of the caller!)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic