File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Illegal Monitor State Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Illegal Monitor State Exception" Watch "Illegal Monitor State Exception" New topic
Author

Illegal Monitor State Exception

Harsha Vardhan Madiraju
Greenhorn

Joined: Dec 19, 2003
Posts: 24
Hi,
In the foll' code, iam sending 5 threads into waiting pool and like to observe the order of their notification. But Im getting Illegal Monitor State Exception. what's wrong?
class NotifyLab
{
static class Rendezvous
{
synchronized public void hurryUpAndWait()
{
System.out.println("*****-------****");
try
{
wait();

}
catch(Exception e)
{
System.out.println(e);
}

}
synchronized public void not()
{
Thread.currentThread().notify();
}
}
static class Waiter extends Thread
{
Rendezvous r;
public Waiter(Rendezvous r,String name)
{
super(name);
this.r =r;
}
public void run()
{
r.hurryUpAndWait();
System.out.println(Thread.currentThread().getName()+" Notified");


}
}
public static void main(String[] args)
{
Rendezvous r = new Rendezvous();
Waiter wt[] = new Waiter[5];
for(int i=0;i<=4;i++)
{
wt[i] = new Waiter(r,"Thread "+(i+1));
wt[i].start();
}
r.not();

}
}
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Moving this to the Threads and Synchronization forum...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

In the "not" method, you're calling "notify()" on a Thread object, but you don't hold the lock on that Thread object; the method is a synchronized method of NotifyLab, so it holds a lock on a NotifyLab object. This exception just means that you've called wait() or notify() on an object whose lock you do not hold.
Just remove the "Thread.currentThread()." part.


[Jess in Action][AskingGoodQuestions]
Harsha Vardhan Madiraju
Greenhorn

Joined: Dec 19, 2003
Posts: 24
I have removed Thread.cureentThread() part, but program isn't working as desired
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Well, I don't know what you want it to do, but here's what will happen: all five threads will be created and call "wait", and at most one of them (but often none!) will wake up when "not" is called. The "notify" call only causes one (more or less randomly-chosen) thread to return from a wait() call on the notified object. It does nothing at all if, as you do here, you call notify() before wait() has even been called. Note that there is a time delay between when you call start() and when run() is actually called; it takes a lot of computation to create and start a new thread. If your notify() call happens before any of the threads have gotten started, then the notify() won't wake up any thread at all.
I can't really tell you how to "fix" this program, as it's not really designed to do anything -- it's just an experiment of some kind, right?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Illegal Monitor State Exception
 
Similar Threads
Question about Thread (using the wait() and notifyAll()/notify() methods in a synchronized block)
Beginner: Threads vs. Runnables
From Velmurugan's Notes
Multithreading: Sun Web Learning Center
What's wrong with this code? (trying wait and notifyall for first time...)