• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Wait() IllegalMonitorStateException

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class BigExten{

void notifyEveryone(){
Thread.currentThread().notify();
}

public static void main(String args[]) throws Exception{
synchronized(System.out){
Thread.currentThread().wait();
}
}
}


The above program throws IllegalMonitorStateException can someone explain ?.



 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main thread should hold a lock on Thread.currentThread and not on System.out. Before calling a wait on an object, the thread should acquire that object's lock.
 
Thiyagarajan Kamala
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nitsh,
class BigExten{

public static void main(String args[]) throws Exception{
synchronized(Thread.currentThread()){
Thread.currentThread().wait();
System.out.println("done");
}
Thread.currentThread().notify();
}
}


I modified to lock on Thread.CurrentThread() but notify is not happening .....
Appreciate your response.
Thanks.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you suppose to call notify when the code halts at the wait statement ;) ...

Also, please put your code between code tags.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the main thread is waiting means its not proceeding any further means it won't reach the notify at all.
 
Thiyagarajan Kamala
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,
I modified the wait() method to wait on the someother object (say G not current thread)...Is the current thread then free to go and execute notify or the main thread itself will go to waiting state..Please clarify .
Thanks.


 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are calling a wait on an object then the thread should acquire the lock on that object means synchronized on that object. So if you are waiting on G then it should synchronized on G and also while notifying, it should be synchronized on G and then call the notify.
 
Thiyagarajan Kamala
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Evenafter synchronizing on g in both wait and notify call the notification is not happening.Can you give the detail on where Iam wrong.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmmmm here the main thread wait on an object BigExten.g so it will wait. Then if the main thread is waiting how will it reach bigExten.notifyEveryone(bigExten.g) which is there in the same thread?
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to modify the above code thinking that if notify is called from a different thread then the wait would interrupted but I was wrong.
Have a look at the code.



The output comes as follows:
going to sleep
started
Going to notify
from thread
notified from main

I suspected "done" would be printed after being notified but its not.
Can anyone tell me what is the basic thing I am missing here?
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I suspected "done" would be printed after being notified but its not.



I can't understand two things in your code.

1. There are two different bigExten references in your main method. So the notifyEveryone method is called with a different object than the one on which main method synchronizes. I've marked it here



2. Even if you notified the correct thread, why would there be an InterruptedException?? Notifying threads waiting on an object doesn't throw an exception in the target threads. Instead the wait method call in the waiting threads return normally. InterruptedException is only thrown if a waiting thread is interrupted using the Thread.interrupt() method...
 
Akanksha Mittal
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit.
I think its clear now.
 
A magnificient life is loaded with tough challenges. En garde tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic