Difference Between Thread.start() and Thread.run()
In the Java programming language, the multi-threading concept consists of the start() and run() methods.
Thread.start():
The thread's execution is initiated by invoking the start() method. The start() method operates two threads they are: current thread and other threads.
The run() is executed by the start method or by calling itself.
The tasks performed by the start thread are:
- Starting of a New Thread.
- Moving the thread from new to runnable state.
- The thread's target run() method will be called once it has a chance to execute.
Example Program:
//program for implementing the start method using Thread class
class TStart extends Thread
{
// method for running the thread
public void run()
{
System.out.println(“Running State of Thread”);
}
}
class Ex
{
// main method
public static void main(String args[])
{
TStart t1 = new TStart();
// using the start method on thread
t1.start();
}
}
Output:
Running state of Thread
Example 2:
// program for start() method using Runnable Interface
class Tstart implements Runnable
{
public void run()
{
System.out.println(“Running of Thread”);
}
public static void main(String args[])
{
Tstart r1 = new Tstart();
Thread t1 = new Thread(r1);
t1.start();
}
}
Output:
Running of Thread
Thread.run():
If a thread was created using a separate Runnable object, the thread class's run() method will be invoked; otherwise, it will do nothing and return. The code supplied in the run() method is performed when the run() function is called. The run() method can be executed repeatedly.
The run() method can be invoked by start() method or by itself.
Program:
//program for implementing the run by start method
class TStart implements Runnable
{
// method for running the thread
public void run()
{
System.out.println(“Running State of Thread”);
}
}
class Ex
{
// main method
public static void main(String args[])
{
TStart r = new TStart();
Thread t1 = new Thread(r);
// using the start method on thread for invoking run method
t1.start();
}
}
Output:
Running state of Thread
Example 2:
// program to implement the run() method directly.
import java.io. *;
import java. util. *;
public class Main extends Thread
{
public void run()
{
System.out.println("Running State of Thread");
}
public static void main(String args[])
{
Main t1=new Main();
// directly using the run() method
t1.run();
}
}
Output:
Running State of Thread
Difference between Thread.start() and Thread.run().
The difference between Thread.start() and Thread.run() are:
Start() | Run() |
The start method is defined in thread class. | The run method is defined in runnable interface. |
The creation of thread and running run method operations are done by using the start method | Only the execution of the run method and thread is cant be created here. |
The start method can only be invoked once throoughout the program | The run method can able to executed many times. |
- Creation of new Thread:
A new thread is generated when a programme invokes the start() method, and the run() method is then called. However, if we explicitly call the run() function, no new thread will be formed; instead, the run() method will be called as a regular procedure on the thread that is now calling; multi-threading will not occur.
Example Program:
//program for implementing the start method using Thread class
class TStart extends Thread
{
// method for running the thread
public void run()
{
System.out.println(“Current Thread:”+Thread.currentThread().getName());
System.out.println(“Run() method called”);
}
}
class Ex
{
// main method
public static void main(String args[])
{
TStart t1 = new TStart();
// using the start method on thread
t1.start();
}
}
Output:
Current Thread name: Thread-0
Run() method called.
In the above program, by calling the start method we are able to create a thread. Whereas when by calling the run() we cannot able to create a thread. The program to run the run() method directly is:
// program for run method
class TStart extends Thread
{
// method for running the thread
public void run()
{
System.out.println(“Current Thread:”+Thread.currentThread().getName());
System.out.println(“Run() method called”);
}
}
class Ex
{
// main method
public static void main(String args[])
{
TStart t1 = new TStart();
t1.run();
}
}
Output:
Current Thread: Main
Successfully executed Run() method
- Multiple Execution:
The start() method can only be invoked once in the program, but the run() method can able to invoked many times.
The example for invoking the start() method multiple times.
Example Program:
import java.io.*;
//program for implementing the start method using Thread class
class TStart extends Thread
{
// method for running the thread
public void run()
{
System.out.println(“Current Thread:”+Thread.currentThread().getName());
System.out.println(“Run() method called”);
}
// main method
public static void main(String args[])
{
TStart t1 = new TStart();
// using the start method on thread
t1.start();
t1.start();
}
}
Output:
Exception in thread "main" java.lang.IllegalThreadStateException
at java.base/java.lang.Thread.start(Thread.java:794)
at Main.main(Main.java:18)
Current Thread:Thread-0
Successfully executed run method
In the above program, the invoking of the start() method many times, it raises an exception.
The Program for executing the run() method multiple times.
Example:
// program to implement the run() method directly twice.
import java.io. *;
import java. util. *;
public class Main extends Thread
{
public void run()
{
System.out.println(“Current Thread:”+Thread.currentThread().getName());
System.out.println(“Run() method called”);
}
public static void main(String args[])
{
Main t1=new Main();
// directly using the run() method
t1.run();
t1.run();
}
}
Output:
Current Thread: Main
Successfully executed Run() method
Current Thread: Main
Successfully executed Run() method
In the above program, the run method is executed twice and it doesn’t show an exception.
Summary:
The start and run are the methods which are used in the Thread concepts. The main differences between start and run methods in the thread are “creation of the thread” and “Multiple Execution”.
The start method can able to create a thread. It is the initial step for the creation of the thread. The start method can able to invoke the run method.
The run() method cannot able to create a thread. The run() method is defined in the Runnable interface. The run() method can be executed many times.