[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

IllegalMonitorStateException

Posted on 2000-04-30
8
Medium Priority
?
503 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 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Expert Comment

by:adam923
ID: 2765268
please explain why you wish to delete the question?
0
 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month20 days, 1 hour left to enroll

873 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