Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads - Yielding

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
RHE page 230 Ex.6

public class TestThread2 extends Thread {
public void run(){
System.out.println("Starting");
yield();
System.out.println("Done");
}
public static void main(String args []){
TestThread2 tt = new TestThread2();
tt.start();
}
}
I ran this code and confirmed boths lines from the run(); are printed.
I realized Calling yield(); only moves the running thread to the Ready state If there�s
a higher priority thread in the waiting pool. Can anyone confirm that??
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to me you are wrong.
My explanation:
Calling yield() moves the thread to the waiting state. After it moved to the ready state, if there are are no high priority threads waiting to be run, the the current thread will resume running.
Guys, correct my if I am wrong.
JAI
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes..JOY..u r right.
Here , there is no 2nd thread ..that is why ..it is printing both.
Hope that helps.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No Joi I Dont Agree With U. Yield methods just free all the resources and send the thread to the ready-to-run state and then its execution depends upon the operating system's thread scheduler.
Thanks

 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Naveen is correct. <code>yeild()</code> lets the scheduler know the thread is willing to let another thread run in it's place. In the end the scheduler will decide which thread gets control. It could be a higher priority thread, a thread with the same priority, one with lower priority or the original thread. The behaviour depends on how the scheduler is implemented on the system. There are no guarantees.
Leonardo, in your example, only one thread is active so <code>yield()</code> behaviour can't truly be seen. Try writing some code that creates a number of threads with different priorities and have them yeild. Of course, the outcome will depend on your platform and won't necessarily occur on all platforms.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
The only thing that kept the leeches off of me was this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic