• 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

Why am I getting IllegalMonitorStateException?

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code mentioned below is a self created one.
The problem is when I run this, I get following exception.
Exception in thread "abc" Exception in thread "abc" java.lang.IllegalMonitorStateException: current thread not owner at java.lang.Object.wait(Native Method) at java.lang.Object.wait(Unknown Source) at com.rajshekharpaul.ThreadA.run(ThreadingTest2.java:12) java.lang.IllegalMonitorStateException: current thread not owner at java.lang.Object.notifyAll(Native Method) at com.rajshekharpaul.ThreadB.run(ThreadingTest2.java:28)

Can anyone help me out in understanding the root of the problem?
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have synchronized on name

So, you should call wait() on name object


But you have called

and you do not have lock on this. that's why.
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Punit for the quick reply.
You corrected my misconception.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajshekhar Paul wrote:Thank you, Punit for the quick reply.
You corrected my misconception.



Welcome Rajshekhar
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have one more doubt.
Suppose the code is working fine with wait() and notifyAll() methods being called at name object.
So, in such cases, when the program is run, is there any possiblity that sometimes, the output will have A is notified?

I tempt to think so because, when ThreadA starts first, it will go to wait only to be notified by ThreadB. In that case, the output will be
Inside A
Inside B
Notify
A is notified
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
change your main() method first.


 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again a silly mistake from my side.
Anyway, thanks again!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic