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.
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.
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.
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
Post by:autobot
World domination requires a hollowed out volcano with good submarine access. Tiny ads are optional.
a bit of art, as a gift, the permaculture playing cards