Solved

IllegalMonitorStateException

Posted on 2000-04-30
8
480 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

733 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