Thread class contain a run() method which has empty implementation. So if we are using multithreading then we must override run() from Thread class.
If we override a Thread.run() in out class then the child thread is responsible to
execute this method.
If we don’t override a Thread.run() method in our class then it will call run() method from Thread class, which has no implementation. So no utput will get.
run() method won’t create a new Thread.
Overloading run() method
We can overload a Thread.run() method in our class. But Thread.start() always call no-argument run() method only. So if you want to call overloaded argument method, you have to call it explicitly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class MyThread extends Thread { public void run() { System.out.println("no-arg run method"); } public void run(int i) { System.out.println("arg method"); } } class Main { public static void main(String[] args) { MyThread t=new MyThread(); t.start(); } } |
Output:
1 |
no-arg run method |
Overriding run() method
We must override a run() method if we want to use multi-threading. If we don’t override a run(), then start() call Thread.run() which has empty implementation. so will not get any output.
1 2 3 4 5 6 7 8 9 10 |
class MyThread extends Thread { } class Main { public static void main(String[] args) { MyThread t=new MyThread(); t.start(); } } |
Output:No output