This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Difference between Thread.yield() and sleep(0) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Difference between Thread.yield() and sleep(0)" Watch "Difference between Thread.yield() and sleep(0)" New topic
Author

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

Abhinav Anand
Ranch Hand

Joined: May 02, 2002
Posts: 113
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

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18500
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between Thread.yield() and sleep(0)
 
Similar Threads
Sleep and Yield state
Thread sleep() method doubt
Thread.yield vs sleep
Note on Thread
Monitors?