• 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

Another question on Synchronization and Locks

 
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all. Happy Holidays!
The code below compiles just fine. However, if I change line 46 and line 58 to use another object as lock, for example, lock1 or lock2 declared at line 40 and 41 respectively, I get an IllegalMonitorStateException. In my previous post I swear I was able to use another object as lock (on a different code). I dont understand exactly what I'm doing wrong.
Command-line parameters I used are the following:
java CubbyHoleTest The Quick Brown Fox Jumped Over The Lazy Dog

Any help is greatly appreciated. Thanks!
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Denis, when you call wait(); in your code, it's equal to calling this.wait(); where this is a monitor of currently executing object. No wonder that when you synchronize your code on an object other than this, you get IllegalMonitorStateException on this.wait(); - the current thread doesn't own a lock on this object. To fix the problem, call wait() on the object used for synchronization. You may find Question on synchronized useful.
Hope this helps.
[ December 25, 2003: Message edited by: Vad Fogel ]
 
dennis zined
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vad. You're the man! Thanks a bunch.
Is the correct answer to your practice question : B, F, G?
thanks.
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dennis zined:
Hi Vad. You're the man! Thanks a bunch.
Is the correct answer to your practice question : B, F, G?
thanks.


Should be B, F, H. G is incorrect because, thanks to the runtime exceptions, the current thread looses its lock letting another thread give it a try (well, not a really successful try ). So, all of the created threads will eventually run. H is correct because throwing a runtime exception allows for the program to finish. If you had sb.wait() instead, both threads would enter run() and get blocked there on waiting forever. There is no code provided to notify them.
reply
    Bookmark Topic Watch Topic
  • New Topic