Thread class run() method code is as shown below.
public void run() {
if (target != null) {
target.run();
}
}
Above target set in the init() method of Thread class and if we create an instance of Thread class as new TestThread(), it’s set to null. So nothing will happen if we don’t override the run() method. Below is a simple example demonstrating this.
public class TestThread extends Thread {
//not overriding Thread.run() method
//main method, can be in other class too
public static void main(String args[]){
Thread t = new TestThread();
System.out.println("Before starting thread");
t.start();
System.out.println("After starting thread");
}
}
Output:
It will print only below output and terminate.
Before starting thread
After starting thread