Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Illegal Monitor State Exception

 
Harsha Vardhan Madiraju
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Threads and Synchronization forum...
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Harsha Vardhan Madiraju
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have removed Thread.cureentThread() part, but program isn't working as desired
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic