Thread Class public void start() method creates a new thread and causes this thread to begin its execution by invoking its run() method which performs a separate job that the thread is supposed to perform. Basically, we start a thread by calling start() function.
Start() function does the following:
- Creates a new thread and evaluates arguments.
- Initialize the parameters.
- Executes method body by invoking run() method.
Override start() method of thread
When we override start() method, JVM executes it like a normal method call and it does not create a new thread. Hence it is not completely logical to override start method. Otherwise, do not go for multi-threading concept.
// We create our own class named MyThread that extends Thread class class MyThread extends Thread { // overridden start method public void start() { System.out.println("start method is running"); } // overridden run method public void run() { System.out.println("run method is running"); } } // Main class public class Demo { public static void main(String args[]) { // Creating a thread object of our class named MyThread and starting the thread. MyThread thread = new MyThread(); thread.start(); System.out.println("main method is running"); } }
Since we have overridden start() method, Thread class start() method does not get executed and a new thread is not created. That means, the main method calls this start() method and executes it. New thread is not created and hence run() method will remain unexecuted. Below is the output of the above program.
Output:
start method is running main method is running
However, we can create new thread by calling super class start() method inside overridden start() method. Look at the implementation below for better understanding.
// We create our own class named MyThread that extends Thread class class MyThread extends Thread { // overridden start method public void start() { // calling super class start method super.start(); System.out.println("start method is running"); } // overridden run method public void run() { System.out.println("run method is running"); } } // Main class public class Demo { public static void main(String args[]) { // Creating a thread object of our class named MyThread and starting the thread. MyThread thread = new MyThread(); thread.start(); System.out.println("main method is running"); } }
Super.start() method calls start() method of thread class, which creates a new thread and starts its execution by calling its run() method. In this case, we can get 3 different outputs according to the order of execution created by thread scheduler.
Output 1:
start method is running run method is running main method is running
Possible Output 2:
start method is running main method is running run method is running
Possible Output 3:
run method is running start method is running main method is running
Note that main method will always execute ‘main method is running’ statement after the execution of ‘start method is running’ statement. Because the main method calls overridden start() method and after execution of that start() method, it prints the statement.
Difference between start() and run() method of Thread class
Both start() and run() provide ways to create multi-threaded programs in Java. The start() method starts the execution of a new thread and calls its run() method for accomplishing specific tasks assigned to that thread. Start() method returns immediately as the new thread normally continues until the run() method ends its execution.