GeeCON Prague 2014*
The moose likes Threads and Synchronization and the fly likes yield() ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "yield() ???" Watch "yield() ???" New topic
Author

yield() ???

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi,

please anyone can tell ,what is the need of yield() of thread class?

Thanks & Regards,
seetharaman.v
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

This is what the javadoc says:

Causes the currently executing thread object to temporarily pause and allow other threads to execute.


What is it that you did not understand?


apigee, a better way to API!
Rajshekhar Anand
Greenhorn

Joined: May 22, 2008
Posts: 14
I have an doubt .
sleep(0) & yield , do the same thing ?


-Gaurav
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Well, yield() conveniently does not throw InterruptedException, making calling code simpler.

In fact, both Sleep(0) and yield() are very rarely necessary. Modern OS will share the CPU fairly between processes anyway.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Except possibly in the case of very tight loops. yield() will make the thread behave more responsibly and prevent it hogging the processing time. As Peter says, Operating Systems are getting better at managing threads, but sometimes it is nice to provide hints.
Ashish Vijay Joshi
Greenhorn

Joined: Nov 27, 2006
Posts: 10
Although dependent on particular VM threads implementation policy, largely the intention of yield() is to take currently running thread and give a chance from runnable pool to any thread with same priority as of thread being swaped from running.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Ashish:
give a chance from runnable pool to any thread with same priority


Same or higher priority I would say.
Ashish Vijay Joshi
Greenhorn

Joined: Nov 27, 2006
Posts: 10
Yes same or higher priority is more appropriate.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Ashish Vijay Joshi:
Yes same or higher priority is more appropriate.


In fact, the Java API doesn't say anything like this, and I believe that is deliberate.

It is not safe to assume that lower-priority threads will not run at all, while there are higher-priority threads with work to do. Many threading implementations might be like that, but others may give a little bit of time to the lower-priority threads - possibly aiming to avoid CPU starvation. And some take no notice of the priority at all; this is perfectly legal behaviour for a JVM.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

Originally posted by Peter Chase:

It is not safe to assume that lower-priority threads will not run at all, while there are higher-priority threads with work to do. Many threading implementations might be like that, but others may give a little bit of time to the lower-priority threads - possibly aiming to avoid CPU starvation. And some take no notice of the priority at all; this is perfectly legal behaviour for a JVM.


In fact, Windows does this. The effective priority of a thread is a combination of its actual priority with a ton of other factors -- including the time it last ran. And yes, this is to prevent thread starvation.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
GeeCON Prague 2014
 
subject: yield() ???