I have created one demo class to test how to create thread by extending thread class. As we need to invoke start()
on class object to call run()
, but if I invoke run()
directly by invoking it on class object it also get run. As it gets run then why should we call start()
on class object to invoke run()
public class Derived extends Thread{
public void run()
{
System.out.println("Run.....");
}
public static void main(String arg[]){
Derived derivedThread = new Derived();
derivedThread.start();
}
}
Above is the valid way, but when I call run() directly like,
public class Derived extends Thread{
public void run()
{
System.out.println("Run.....");
}
public static void main(String arg[]){
Derived derivedThread = new Derived();
//derivedThread.start();
derivedThread.run();
}
}
Above code also run successfully.
Then why we call thread by first way. Can somebody clear it? Will appreciate your help.