This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what all ways a Thread can leave a lock on Object Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "what all ways a Thread can leave a lock on Object" Watch "what all ways a Thread can leave a lock on Object" New topic
Author

what all ways a Thread can leave a lock on Object

Vivek Nidhi
Ranch Hand

Joined: Aug 10, 2003
Posts: 133
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
David Hadiprijanto
Ranch Hand

Joined: Sep 14, 2003
Posts: 52
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

Joined: Aug 10, 2003
Posts: 133
Thanks Boss
regs
Vivek Nidhi
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: what all ways a Thread can leave a lock on Object
 
Similar Threads
wait() & notify()?
synchronization in java
ReentrantLock - lock vs lockInterruptibly
Want help...
ready to upload,but...