aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how to release the lock?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how to release the lock??" Watch "how to release the lock??" New topic
Author

how to release the lock??

Sun LiWei
Ranch Hand

Joined: Aug 10, 2002
Posts: 49
are there some other ways for a thread release the lock of a object except call wait() method??
Panagiotis Varlagas
Ranch Hand

Joined: Nov 27, 2000
Posts: 233
Yes Sun there are. (Sun as in Sun LiWei, not as in Sun Microsystems... ).
Consider the following piece of code:

Imagine that the myMonitor object is not locked by any thread and a thread (let's call it T) attempts to execute the above piece of code. Since the monitor is unlocked, T is able to get hold of it (locks it) and starts executing the code within the synchronized block. As soon as T exits the synchronized block, it releases the lock it holds on myMonitor; thereafter, myMonitor is free of locks and available for locking by another thread that can manage to get hold of it.
The difference this scenario has with an invocation of wait() is that a thread invoking wait() on a monitor, would result in the thread being added to the wait set of the monitor, and a subsequent invocation of notify() would result in the thread moving back to a Ready state and competing for locking the object. (don't forget that wait() HAS to be invoked within a synchronized block). On the other hand, when a thread exits a synchronized block it (1) releases the lock and (2) gets out of the competition for the particular monitor altogether.
Hope this helps,
Panagiotis.
SCJP2, January 2001 (91%)
IBM Certified Solution Developer VAJ, May 2001
(After that I went on a "certification sabbatical" of sorts )
[ September 12, 2002: Message edited by: Panagiotis Varlagas ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
The monitor is also released if the method completes abruptely.


SCJP2. Please Indent your code using UBB Code
pree sree
Ranch Hand

Joined: Jul 27, 2002
Posts: 34
yield() puts back the running thread to ready state and wait() puts the thread in the waiting pool of the monitor.both are allowing other threads to execute.then why 2 methods to do the same thing?
pree sree
Ranch Hand

Joined: Jul 27, 2002
Posts: 34
yield() puts back the running thread to ready state and wait() puts the thread in the waiting pool of the monitor.both are allowing other threads to execute.then why 2 methods to do the same thing?
is the thread being blocked same as wait()?
[ September 13, 2002: Message edited by: pree sree ]
Sabarish Sasidharan
Ranch Hand

Joined: Aug 29, 2002
Posts: 73
wait() will help in co-ordinating access of shared resources between threads running parallelly. It should be used along with notify()/notifyAll() to achieve this. This is its main objective.
But yield() will just put the thread back into the ready queue. It won't help in synchronising access to a shared resource.
Both do prevent the current thread from continuing to run, but each has a different purpose. In the case of wait() this is a side-effect. But in the case of yield(), it is the main purpose itself.
Again do note that yield() is not guaranteed to allow other threads to execute. Its behaviour depends a lot on the threading model of the operating system.
As Marcus rightly reminds us "Garbage collection and threading are 2 areas where java is platform dependent"


Sab<br /> <br />Perfection does not come from belief or faith. Talk does not count for anything. Parrots can do that. Perfection comes through selfless work.<br />Swami Vivekananda
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
If you're holding a lock, yield() does not release it.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to release the lock??
 
Similar Threads
yield() in synchronized code - a Dan Chisholm question
Thread Monitor
Sleeping thread takes locks with it?
question on wait()
question from Jquest on Threads