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.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Thread" Watch "Thread" New topic
Author

Thread

jeena jose
Ranch Hand

Joined: May 06, 2001
Posts: 69
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
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
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
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
hi,
some one can clear my doubt with a piece of code.
jeena jose
Ranch Hand

Joined: May 06, 2001
Posts: 69
hi,
some one help me!!!
to clear my doubt with a piece of code.
Nagarajan Subramanian
Greenhorn

Joined: Mar 18, 2001
Posts: 11
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread
 
Similar Threads
Doubt on Thread.sleep()
Doubt in Thread Chapter
Thread doubt
scjp self test on thread doubt
Small Doubt on Threads