• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread.yield vs sleep

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .

 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 89
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 87
Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nidhi Singhal wrote:So definitely the monitor lock is relinquished.


Wrong ... completely wrong .... Thread will keep the lock.
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may want to look here -
Waiting and Yielding in JAVA
 
reply
    Bookmark Topic Watch Topic
  • New Topic