• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

thread question

 
weiliu lili
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • 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?
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • 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
 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do we actually doing here in this Runnable object?
 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic