Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Thread execution

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Ranchers
Can anybody explain why this code produce output End?
Why it does not print sop inside run() method?


public class Question17 {
public static void main(String[] args) throws Exception{
Thread t1 = new Thread(getRunnable(3));
Thread t2 = new Thread(getRunnable(4));
t1.join();
System.out.println("End");
}

public static Runnable getRunnable(final int id){
return new Runnable(){
public void run(){
for(int i=0;i<id;i++){
System.out.print(" "+i);
}
}
};
}
}

Thanks in Advance
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do these run methods ever execute?

(Remember to cite your sources when posting mock exam questions, and please use Code Tags. )
 
Mukul Vashishtha
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
Marc
Can we call run() method inside getRunnable() method?
If yes please tell me how can we do this?

Thanks in Advance
 
Author
Posts: 3450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to call the t1.start() or t2.start() for a thread to run and execute the thread run() method. You cannot directly call the run() method.
 
Mukul Vashishtha
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
Mr. Pillai.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by arulk pillai:
... You cannot directly call the run() method.


You certainly can call the run() method directly. However, it will then execute in the current thread instead of as its own thread. You need to be aware of this behavior for the exam.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mukul Vashishtha:
...Can we call run() method inside getRunnable() method? ...


The getRunnable method returns an instance of Runnable. This instance is not a thread until it's passed to a Thread constructor, and it's the thread (not the Runnable) that you call start() on.
[ July 02, 2007: Message edited by: marc weber ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic