Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help!!!!! IllegalMonitorException thrown!!!  RSS feed

 
juan velez
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All. I know that there has been some answers to this exception, but it seems to be that it does not apply to my case. My code is fairly small (that is what I think) . When I execute this program, I get the IllegalMonitorStateException. Could anyone point me where I am doing wrong? I appreciate it.
Thanks
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you want is Thread.interrupt(), not notifyAll().
 
juan velez
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry, but what would i want to interrupt the thread? What I am trying to do is a case of "CONSUMER-PRODUCER". If there is not a "thing" to consume, I should wait() until someone notifyAll() that there is something to consume. If I were to interrupt the thread, I would expect to terminate, but that is not the case. Thanks anyway
 
juan velez
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the bug. I swear that I had written the synchronized modifier for all those methods that make use of wait() and notifyAll() methods. Including the synchronized keyword to all wait() and notifyAll() client methods, my program just runs fine.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the docs, you have to own the object's monitor before you call these methods:

This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:
1. By executing a synchronized instance method of that object.
2. By executing the body of a synchronized statement that synchronizes on the object.
3. For objects of type Class, by executing a synchronized static method of that class.
Only one thread at a time can own an object's monitor.

For more on synchronizing threads please see this link.
[ September 09, 2002: Message edited by: Anthony Villanueva ]
 
juan velez
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anthony: Indeed, if you take a look to my code, I was missing the synchronized keyword from two methods: public String getError() and publiv void putNewError(String). Inside these two methods, I am calling the "wait()" and "notifyAll()" methods that need to be "wrapped" around synchronized methods. Again, it seems to be that I overlooked the fact that that important keyword was not present for all methods where wait() and notifyAll() are being used.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!