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???
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.
Joined: May 06, 2001
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???
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