Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about yield

 
jaman tai
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jaman tai
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think Barry is correct! because yeild is static method of Thread, current thread should pause and that is main.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic