Forums Register Login

yield() ???

+Pie Number of slices to send: Send
Hi,

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

Thanks & Regards,
seetharaman.v
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
I have an doubt .
sleep(0) & yield , do the same thing ?


-Gaurav
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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



Same or higher priority I would say.
+Pie Number of slices to send: Send
Yes same or higher priority is more appropriate.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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
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
https://gardener-gift.com


reply
reply
This thread has been viewed 1103 times.
Similar Threads
Thread - yield()
thread Q of Dan's Mock Exam
log files.
Thread Question!
Thread again..
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 14:16:52.