aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes thread question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "thread question" Watch "thread question" New topic
Author

thread question

weiliu lili
Ranch Hand

Joined: Apr 11, 2002
Posts: 46
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

Joined: Jan 08, 2002
Posts: 411
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

Joined: Aug 29, 2001
Posts: 219
What do we actually doing here in this Runnable object?


Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: thread question