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?
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.
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.