Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

what all ways a Thread can leave a lock on Object

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello there
Suppose a Threads enter a sychronised block, the lock is release when the Thread dies in it and also when the thread comes out of the sychronised block.
ANY OTHER CASES THE LOCK CAN BE RELEASED
Vivek Nidhi
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way the lock can be release if the thread invoke a wait method of the same object whose lock is the same.
The code below if run as expected will result in :
Thread A - waiting to be notified.
Main Thread: About to notify a.
Thread A - has been notified.
It demonstrates that A releases the lock (of itself) when it invokes the wait() method and allow the Main Thread to enter the synchronized(a) block.

P.S. : There is a little guarantee in Thread behavior, hence, I am undermining the sleep() method in the code above for the sake of demonstration only.
 
Vivek Nidhi
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Boss
regs
Vivek Nidhi
 
Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic