Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Why am I getting IllegalMonitorStateException?

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
change your main() method first.


 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again a silly mistake from my side.
Anyway, thanks again!
    Bookmark Topic Watch Topic
  • New Topic