Solved

IllegalMonitorStateException

Posted on 2000-04-30
8
445 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

708 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

11 Experts available now in Live!

Get 1:1 Help Now