This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
current thread get a lock on the object when it executes a synchronized method.the lock will be released when the thread executes wait() method or it returns the method. my doubt is: whether the lock will be released when the thread executes yeild() or other methods that cause the thread to change from the running state???
second doubt: when one thread calls sleep() method, will thread shedular allow another thread to execute???
Charlie Swanson
Ranch Hand
Joined: Jan 29, 2001
Posts: 213
posted
0
http://java.sun.com/j2se/1.3/docs/api/index.html See the above documentation. The sleep does not loose ownership of any monitors. A call to yield causes the currently executing thread to move to the Ready state if the scheduler is willing to run any other thread in place of the yielding thread. I wrote a small piece of code and put "yield" inside of synchronized code. It never gave up the monitor. At the same time I had another piece of synchronized code waiting for the object. It never ran.
jeena jose
Ranch Hand
Joined: May 06, 2001
Posts: 69
posted
0
boone in java2 exam guide in page 300 stating that "the lock can be released when a synchronized method executes wait(),yeild(),or other methods that cause the threda to change from running state." is this statement wrong???
Ash sav
Ranch Hand
Joined: Apr 14, 2001
Posts: 55
posted
0
When the method called wait() or yield() thread goes to Ready_to_run state (or waiting state after wait() call)that is it is not in the running state. So it has to release the lock.
jeena jose
Ranch Hand
Joined: May 06, 2001
Posts: 69
posted
0
hi, some one can clear my doubt with a piece of code.
jeena jose
Ranch Hand
Joined: May 06, 2001
Posts: 69
posted
0
hi, some one help me!!! to clear my doubt with a piece of code.
Nagarajan Subramanian
Greenhorn
Joined: Mar 18, 2001
Posts: 11
posted
0
Hello Jeena, From what I could gather from Khalid Mughal, the yield() method is used to relinquish processor resources (CPU). I do not think it will relase the lock on the monitor. This is more to do with processor resource sharing rather than object locks I believe. So if a thread is waiting for an object lock and a second thread has the object lock, executing the yield() method in the second thread should not relase the object lock. Somebody please correct me if wrong. Thanks