Payal that is correct a thread that has a lock on an object that calls wait or yield will release the lock. wait can only be called from within synchronized code. keep in mind that a thread that calls yield may start running again right away if the scheduler doesn't find any threads it would rather run. A thread that calls sleep will not give up the lock. hope that helps Dave
[This message has been edited by William Brogden (edited July 27, 2001).]
Joined: May 10, 2001
Bill I thought that yield automatically put the thread into the ready state - it doesn't release the lock when it does this? I know the API specifically says sleep doesn't release the lock, yield doesn't say it doesn't lose it so I assumed it lost it. Is that what I get for assuming?
I am with Bill.Only the wait() method can release the lock for an object. Yield() is used to pass the control to the ThreadScheduler, which decides what to do with the thread.However, there is no monitor to release the lock of an object, when you call the Thread.yield() method. The Object is locked only by using the synchronized keyword and wait and notify mechanisms. Hope this helps, Sandeep SCJP2,OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
Author and all-around good cowpoke
Joined: Mar 22, 2000
"Does it mean that yield will not release the lock and those waiting for that lock will never get the lock and will not run then how come the yielded method will get its turn to run." When the yield returns, the Thread having the lock continues. Bill
Yes only the wait() and yield() meths can release an object's lock coz these meths r used for interthread communicatin and so for other threads to access the object the current thread must give up the lock.As for sleep(),since it may modify the object's state after it exits out of sleep() it must hold the object's lock.Hope u uderstand . All the best for the exam! Let me know afterwards ur score and the type of questions asked. my mailid is email@example.com
Joined: May 01, 2001
So, which option should I opt?? more comments needed.
Joined: Apr 02, 2001
Hi, Thread.yield() will not release the lock the thread holds, only wait() can! Payal, you need to go with Bill.Do you have any other option! -- Sandeep SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)