[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

IllegalMonitorStateException

Posted on 2000-04-30
8
Medium Priority
?
500 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 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
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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

649 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