Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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: 3461
  • 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 ]
 
I was her plaything! And so was this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic