Introduction
As we all know, the two most significant methods of multithreading are start() and run(). The former is used to establish a new thread, while the latter is used to begin running that thread. The two most significant methods in Java's multithreading paradigm are start() and run(). The following are some of the distinctions between Thread.start() and Thread.run().
Let’s take a deep dive
Let us see a few of the major differences between the run and start methods in java.
1. Thread creation: When a programme invokes the start() function, it creates a new thread and then calls the run() method. However, if we call the run() function directly, no new thread is generated, and the run() method is executed as a normal method call on the current calling thread, with no multi-threading. Let's have a look with an example.
Program
class Strand extends Thread{
public void run(){
System.out.println("Current thread: " + Thread.currentThread().getName());
System.out.println("run() is called");
}
}
class CodingNinjas{
public static void main(String[] args) {
Strand strand = new Strand();
strand.start();
}
}
Output
When we call the start() function of our thread class instance, a new thread is created with the default name Thread-0, and then the run() method is called, and everything inside it is executed on the newly formed thread, as seen in the above example. Now let's see the run() method instead of start().
Try it on online java compiler.
Program
class Strand extends Thread{
public void run(){
System.out.println("Current thread: " + Thread.currentThread().getName());
System.out.println("run() is called");
}
}
class CodingNinjas{
public static void main(String[] args) {
Strand strand = new Strand();
strand.run();
}
}
Output
2. Multiple invocation: Another significant distinction between the start() and run() methods in Java's multithreading idea is that the start() method cannot be performed again without throwing an IllegalStateException, but the run() method may be called many times as it is just a regular method call. Let's have a look at an example:
Program
class Strand extends Thread{
public void run(){
System.out.println("Current thread: " + Thread.currentThread().getName());
System.out.println("run() is called");
}
}
class CodingNinjas{
public static void main(String[] args) {
Strand strand = new Strand();
strand.start();
strand.start();
}
}
Output
The java.lang.IllegalThreadStateException is raised when the start() function is used again, as seen in the previous example. Let's try calling the run() function twice now:
Program
class Strand extends Thread{
public void run(){
System.out.println("Current thread: " + Thread.currentThread().getName());
System.out.println("run() is called");
}
}
class CodingNinjas{
public static void main(String[] args) {
Strand strand = new Strand();
strand.run();
strand.run();
}
}
Output
As shown in the above example, invoking the run() function twice does not result in an error, and it is executed twice as intended, but on the main thread.
Must read, Duck Number in Java and Hashcode Method in Java