This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Class Method

 
payal bansal
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
payal bansal
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do tell me the one right ans as i am appearing for SCJP on 31 july. Thanks all!
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, which option should I opt?? more comments needed.
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic