This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

about yield

 
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?
 
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.
 
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!