• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Difference between Thread.yield() and sleep(0)  RSS feed

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

As we know calling sleep from any thread halts the execution of the given thread for a specific period of time without losing the monitor of the thread. On the other hand invoking Thread.yield() causes the currently executing thread object to temporarily pause and allow other threads to execute.

Now my doubt is

1.
Whehther calling Thread.yield() does not lose the monitor of the thread like the sleep() method.

2.
Does sleep(0) behave like Thread.yield(). That is does it return immediately like Thread.yield() after it has allowed other threads to execute.

Well regarding the second point i doubt that it works that way because as far as i know sleep method forces the thread to halt execution for a specific period of time during which other threads are allowed to execute.

What i want is this that without losing any monitor on any object that a thread holds i want that thread to relinquish control of the cpu so that other threads can execute. However if no threads are there to be executed then the control sould immediately return without any dealy.

In a nutshell i want a Thread.yield() behavior without losing any sort of object monitors that the concerned thread might be holding.

Any suggetions

Thanks in advance,
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread.yield() does not give up ownership of monitors, so probably does what you want.

The API does not make it clear what happens for Thread.sleep(0), so it is probably not a good idea to use it (even if you find it does what you want now, it might not in the future).

Do remember that Java doesn't give you very precise control over which thread is really executing, even if you use yield(), sleep(), wait() etc.
 
author
Posts: 23805
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Peter Chase:
Thread.yield() does not give up ownership of monitors, so probably does what you want.

The API does not make it clear what happens for Thread.sleep(0), so it is probably not a good idea to use it (even if you find it does what you want now, it might not in the future).

Do remember that Java doesn't give you very precise control over which thread is really executing, even if you use yield(), sleep(), wait() etc.



sleep() does not give up ownership of locks either... but in a nutshell, yield() and sleep(0) may not be that useful, since I don't think either is guaranteed to actually do anything.

Henry
 
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yield can be usefull as a hint to the thread scheduler. It can enhance thread switching.

I find it rarely useful though and IMHO its a relic form the days of much poorer schedulers.

I would surmise the two are effectively synonomous. Though yield probably is leaner.
[ October 12, 2004: Message edited by: CL Gilbert ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!