Table of contents
1.
Introduction
2.
Let’s take a deep dive
2.1.
Program
2.2.
Output
2.3.
Program
2.4.
Output
2.5.
Program
2.6.
Output
2.7.
Program
2.8.
Output
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024

Difference between run() and start() method

Author Aman Thakur
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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();
   }
}
You can also try this code with Online Java Compiler
Run Code

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();
   }
}
You can also try this code with Online Java Compiler
Run Code

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();
   }
}
You can also try this code with Online Java Compiler
Run Code

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();
   }
}
You can also try this code with Online Java Compiler
Run Code

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

FAQs

1. How will the run () method be called?
Ans: If the thread was created using a separate Runnable object, the run() function of the thread class is invoked; otherwise, this method does nothing and returns. The code supplied in the run() method is performed when the run() function is called. The run() function can be used many times.


2. Can we start the thread twice?
Ans: No, A thread cannot be restarted after it has been begun. An IllegalThreadStateException is raised if you do so. In this situation, the thread will execute once, but will throw an exception the second time.


3. What is a daemon thread in Java?
Ans: In Java, a daemon thread is a low-priority thread that performs background operations such as garbage collection. In Java, a daemon thread is also a service provider thread that helps the user thread.

 

4. What is the difference between synchronised and non synchronised in Java?
Ans: Non-Synchronisation is not thread safe and cannot be shared between many threads without correct synchronisation code. On the other hand, Synchronised is thread-safe and may be shared across several threads.

Key Takeaways

We hope that this blog has helped you understand more about the difference between start and run in Java, and if you want to learn more, check out our Introduction to Java articles.

Recommended Readings:

Head over to our practice platform Coding Ninjas Studio to practise top problems, attempt mock tests, read interview experiences, and much more.Please upvote our blog to assist other ninjas in their development.

Happy Learning!

Live masterclass