File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

start() kicks start() but not run() why?

 
srinivas bolloju
Ranch Hand
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was experimenting on this small program,i found out that start() below kicks off start() and not run(), ,can anybody pls explain me this...
thanks in advance
public class startRun extends Thread{
public static void main(String argv[]){
startRun b = new startRun();
//b.run();
b.start();
}
public void start(){
//run();
for (int i = 0; i <10; i++){
System.out.println("Value of i = " + i);
}
}
public void run(){
System.out.println("im in run");
}
}
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When extending Thread, you just have to provide a body for the run() method, but do not override the native start() method otherwise your thread won't run. Try this:
 
Deepali Pate
Ranch Hand
Posts: 114
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are extending the Thread class and overriding the start() method from the Thread class. The default start() of Thread class has an inbuilt script which would call run().
But now since u are overriding the default start() the overridden start() is called and it has no code to invoke the run().
This is the reason i guess.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic