• 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

Concurrency Gurus, It's for you!

 
Ranch Hand
Posts: 358
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Well, could you tell me why 2 threads are holding the lock? Is it possible?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Well, could you tell me why 2 threads are holding the lock? Is it possible?



Your two threads are *not* holding the lock at the same time.

For output 1, the lock is released by the main thread in the wait() method -- thus allowing thread b to get the lock.

For output 2, thread b holds the lock until it completes the synchronized block. Only when thread b releases the lock does the main thread acquire the lock.

Henry
 
Faisal Ahmad
Ranch Hand
Posts: 358
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the hint!
In output 2, Thread-B has finished and is dead before main thread has started. But, unfortunately, main-thread is still in hopes that Thread-B will run and is waiting for the lock that it has given up.
Well, now comes an interesting question:
How do we get the lock that we've given up earlier intentionally? Let's say, no one is there for us to notify.
[ April 15, 2007: Message edited by: Faisal Pasha ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic