• 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

Help!!!!! IllegalMonitorException thrown!!!

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you want is Thread.interrupt(), not notifyAll().
 
juan velez
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic