aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about yield Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about yield" Watch "about yield" New topic
Author

about yield

jaman tai
Ranch Hand

Joined: Sep 26, 2005
Posts: 37
public class Test1{
public static void main(String args[]){
MyT myt = new MyT();
myt.start();
myt.yield(); //1. who will temporarily pause and allow other threads to execute
}
}

class MyT extends Thread{
public void run(){
System.out.println("MyT.run()");
}
}


after //1, who will temporarily pause and allow other threads to execute? t or main?
Ashwin Kumar
Greenhorn

Joined: Oct 13, 2005
Posts: 27
public class Test1{
public static void main(String args[]){
MyT myt = new MyT();
myt.start();
myt.yield(); //1. who will temporarily pause and allow other threads to execute
}
}

class MyT extends Thread{
public void run(){
System.out.println("MyT.run()");
}
}


It is the thread "myt" that will yield(). Only in case of join, say "myt.join()", the main thread will wait for "myt" to die before terminating.

Anyone correct me, if i am wrong.


SCJP 1.4<br />SCBCD 5.0<br />SCWCD 1.4 (Preparing)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Because yield is a static method in class Thread, myt.yield will not cause the myt thread to yield. It will be the main thread that yields. This is yet another example that proves that static methods should be called through the class, as in Thread.yield(), and not via object references. It's also a case for visiting the API if you have any doubts.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
jaman tai
Ranch Hand

Joined: Sep 26, 2005
Posts: 37
i think Barry is correct! because yeild is static method of Thread, current thread should pause and that is main.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about yield
 
Similar Threads
reader interface, wait() then go
About Thread question?
does yield prevent thread execution?
Thread Answer Error
Thread with synchronized run() method and sleep()