• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Illegal Monitor State Exception

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();

}
}
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the Threads and Synchronization forum...
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have removed Thread.cureentThread() part, but program isn't working as desired
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic