Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Abhinav Anand
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,
 
Peter Chase
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.
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
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
 
Mr. C Lamont Gilbert
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic