• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Concurrency Gurus, It's for you!

 
Ranch Hand
Posts: 355
Redhat Chrome 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: 23907
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: 355
Redhat Chrome 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 ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic