• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

yield() ???

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Thanks & Regards,
seetharaman.v
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rajshekhar Anand
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an doubt .
sleep(0) & yield , do the same thing ?


-Gaurav
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish:
give a chance from runnable pool to any thread with same priority


Same or higher priority I would say.
 
Ashish Vijay Joshi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes same or higher priority is more appropriate.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21184
80
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:

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 Reply
  • Bookmark Topic Watch Topic
  • New Topic