Solved

IllegalMonitorStateException

Posted on 2000-04-30
8
454 Views
Last Modified: 2012-06-22
How to avoid IllegalMonitorStateException with wait?

here is my code:

public class OutServerPool extends Thread {
  OutServer server;
  private Vector v;
public OutServerPool(OutServer server) {
  super();
  this.server=server;
  v=new Vector();
}
public synchronized void close() {
  if (this.isAlive())
    this.stop();
    server=null;
    v=null;
}
public synchronized Message poll() {
  if (v.size()==0)
    return null;
  Message msg=(Message)v.elementAt(0);
  v.removeElementAt(0);
  return msg;
}
public synchronized void push(Message msg) {
  v.addElement(msg);
  this.resume();
}
public void run() {
  while (true) {
    Message msg=null;
    while ((msg=poll())!=null) {
      if (server.oos!=null) {
        try {
          server.oos.writeObject(msg);
        } catch (Exception e) {
          System.err.println(this.getClass().getName()+":"+e);
          server.quit();
        }
      }
    }
    try {
      this.wait(); // raise the IllegalMonitorException always
    } catch(InterruptedException e) {
  }
}
}

Is there something that I don't know about wait() ?

Please help me.
0
Comment
Question by:threshold
  • 5
  • 3
8 Comments
 
LVL 4

Accepted Solution

by:
adam923 earned 100 total points
ID: 2765253
yes there is, you must call wait from within a synchronized method (or block)
if you do otherObject.wait() then it must be synchronized for that other object.
the reason for this is that wait means "give up the lock and pause this thread until notified" so it only makes sense when you have the lock on that object
0
 
LVL 2

Author Comment

by:threshold
ID: 2765263
This question has a deletion request Pending
0
 
LVL 4

Expert Comment

by:adam923
ID: 2765267
This question no longer is pending deletion
0
 
LVL 4

Expert Comment

by:adam923
ID: 2765268
please explain why you wish to delete the question?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Author Comment

by:threshold
ID: 2765461
Sorry, because I found the answer in the other Questions.

And, I try to replace wait/notify with  suspend/resume. It is successful.

Sorry, i hadn't notice your comment when I deleted the question.
0
 
LVL 4

Expert Comment

by:adam923
ID: 2766543
i'd suggest that you not use suspend and resume, they're very dangerous and have since been removed from the language completely.  wait and notify should be all you need for thread management.... suspend and resume will shoot you in the foot quickly!
0
 
LVL 2

Author Comment

by:threshold
ID: 2768712
You r right!

For your suggestion, I fixed the code with wait/notify.

Thank you for your help.
0
 
LVL 4

Expert Comment

by:adam923
ID: 2770580
you're welcome
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now