Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread.yield vs sleep  RSS feed

 
Punya Pratap Singh
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I saw in many explanations that some people say that in sleep() or yield(),don't know which one, but in one of them the thread doesn't relinquish monitor lock.

My question is this, weather it is sleep or yield,first of all in case of syncronization it must relinquish the lock other wise how come the next thread enter in monitor and execute the syncronized method.

and one more confusion that the concept of object monitor only comes in case of syncronized methods or block ?
Am I right.

Please Help .

 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wait releases the lock you are synchonized on (only that lock) , you may not have any locks when you yield / sleep (and probably shouldn't, depends on what your doing) so that will release nothing and yes potentially block other threads if they attempt to acquire the same held lock on the sleeping thread.

The java docs on the API cover this well.
 
Nidhi Singhal
Ranch Hand
Posts: 89
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I saw in many explanations that some people say that in sleep() or yield(),don't know which one, but in one of them the thread doesn't relinquish monitor lock.

My question is this, weather it is sleep or yield, first of all in case of syncronization it must relinquish the lock other wise how come the next thread enter in monitor and execute the syncronized method.

and one more confusion that the concept of object monitor only comes in case of synchronized methods or block?
Am I right.


Hi Punya,

Both yield() and sleep() will relinquish the monitor lock.

Yield() facilitates turn-taking among the equal priority threads. In case if no thread of the same priority is available, yield() will give the control back to same thread (as was executing earlier) almost immediately. So it seems as if the thread hasn't relinquished the monitor lock.

Sleep() is used to delay the execution of the thread for a specified amount of time. So definitely the monitor lock is relinquished.

Regarding your second Q - The object monitor comes into picture both in case of the synchronized method as well as the synchronized block. A monitor (lock) is like a privilege that only one thread can "own" at any point of time (i.e. only one thread can execute within the synchronized block at a time)

Thanks
Nidhi
 
prem pillai
Ranch Hand
Posts: 87
Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nidhi Singhal wrote:So definitely the monitor lock is relinquished.

Wrong ... completely wrong .... Thread will keep the lock.
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to look here -
Waiting and Yielding in JAVA
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!