• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

thread question

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Question17 {
public static void main(String[] args) throws Exception{
final int a=3;
final int b=4;

Thread t1 = new Thread(getRunnable(a));
Thread t2 = new Thread(getRunnable(b));
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);
}
}
};
}
}
The running result is "end", the thread doesnot start, why?
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Weilliu
You had to call start() method for thread running.
***************************************
Thread t1 = new Thread(getRunnable(a));
Thread t2 = new Thread(getRunnable(b));
t1.start();
t2.start();
t1.join();
System.out.println("End");
***************************************
Jamal Hasanov
www.j-think.com
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do we actually doing here in this Runnable object?
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the problem, the start() method of both threads doesn't actually get called, so the threads don't get to run at all. The method join() doesn't do anything 'coz thread t1 isn't running.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic