Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The yield() method

 
Bojan Knezovic
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have several threads in the runnable pool and one thread which is running and has priority higher than all those in the pool, what will happen when I invoke yield() method on the running thread?
Will it
1) Leave the running state, return to runnable pool and since it has priority higher than all the rest be elected again, OR
2) It won't even bother changing state since JVM knows that the rest of the threads in the pool have lower priorities?
I have understood this to be a step-by-step process (first solution) but it doesn't seem to be so...?
Some quotes from the K&B book:
pg. 513: "What yield() is supposed to do is make the currently running thread head back to runnable to allow other threads of the same priority to get their turn."
pg. 553: "The yield() method is not guaranteed to cause a thread to leave the running state..."
So will it leave to get back or it won't leave at all?
I'd appreciate if someone could clarify it a bit.
TIA,
Bojan
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want the short answer or the long answer?
The short answer is that you just don't know what it will do.
The long answer is a bit more involved. We already said that we don't know what the results of invoking yield will be. Why not?
Well, remember that your Java application is running in the JRE, not directly in the underlying OS. It is the underlying OS that is responsible for actually handling the threads, not your application. Therefore, invoking yield on a thread is more like a suggestion than an actual command.
The idea would be that the thread leaves the runnable state so that another thread can be started - once another thread is started, the original thread would hop right back into the runnable state and would be available for selection with the next time slice.
Of course, as I already stated, it's really up to the underlying OS to perform this task, not the Java program, itself. The underlying OS can implement threading however it wants.
I hope that helps,
Corey
 
Aneesha Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bojan,
I was studying the same topic just now and this threw me. I am not sure but this is how it seems to work:
"A thread can voluntarily yield the CPU without going to sleep or some other drastic means by calling the yield method. The yield method gives other threads of the same priority a chance to run. If there are no equal priority threads that are runnable, then the yield is ignored. "
So it seems that if all the threads are a lower priority the yield will be ignored in your stated case!
The article from which I got this is http://java.sun.com/docs/books/tutorial/essential/threads/priority.html
Do let me know if you find it to be something absolutely different
:roll:
Thanks!!
[ March 11, 2004: Message edited by: Aneesha Singh ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic