wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Class Method 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 "Thread Class Method" Watch "Thread Class Method" New topic
Author

Thread Class Method

payal bansal
Greenhorn

Joined: May 01, 2001
Posts: 23
Hi all!
Is this a correct statement??? Object's lock acquired by a thread can be released by wait() and yield() method(). and sleep() can not release the lock.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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


Dave
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12675
    
    5
Only wait has a mechanism to release the lock on an object. The yield and sleep methods do not, and neither do the (deprecated)suspend and resume methods.
Bill

------------------
author of:


Java Resources at www.wbrogden.com
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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?

Thanks
Dave
Muhammad Farooq
Ranch Hand

Joined: May 08, 2001
Posts: 356
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.
--Farooq


Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
payal bansal
Greenhorn

Joined: May 01, 2001
Posts: 23
Do tell me the one right ans as i am appearing for SCJP on 31 july. Thanks all!
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
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>
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12675
    
    5
"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
Vedhas Pitkar
Ranch Hand

Joined: Jan 27, 2001
Posts: 445
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 vedhas_pitkar@yahoo.com
payal bansal
Greenhorn

Joined: May 01, 2001
Posts: 23
So, which option should I opt?? more comments needed.
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
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)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Class Method
 
Similar Threads
can a thread having lock of object, simulatenously invoke TWO sunchronized methods ?
Lock - Object or Method
Threads and Synchronization
static synchronized
static synchronized vs non-static synchronized