When to call the Thread.run() instead of Thread.start() in Java?



This article will briefly discuss the run() and start() methods, and also explain when to call run() instead of the start() method.

Calling run() Method Instead of start()

Usually, to execute a thread, we will call the start() method, and the start method calls run() implicitly, and the contents of the run() method will be executed as a separate thread.

We can also call the run() method explicitly instead of the start() method. If we do so, we will be executing the contents of the run method in the current thread but not in a separate one.

In this case, the run() method behaves like a normal overridden method and runs within the context of the current thread. Before moving to the examples, let's quickly discuss these methods:

The run() Method

In Java, the run() method of the Thread class is used to define the sequence of actions that a thread will execute. This method will be invoked by the Java Virtual Machine (JVM) implicitly; we can also invoke this method explicitly in the current thread without calling the start() method.

The start() Method

In Java, the start() method of the Thread class is used to "begin the execution" of a "new thread". It is also used for "multithreading" because it creates a separate call stack, allowing multiple threads to run together and perform tasks in parallel.

Example 1

In the following example, we use the run() method instead of the start(), as we don't need multithreading and simply execute the current thread:

public class CallRunMethodTest extends Thread {
   @Override
   public void run() {
      System.out.println("In the run() method: " + Thread.currentThread().getName());
      for(int i = 0; i < 5 ; i++) {
         System.out.println("i: " + i);
         try {
            Thread.sleep(300);
         } catch (InterruptedException ie) {
            ie.printStackTrace();
         }
      }
   }
   
   public static void main(String[] args) {
      CallRunMethodTest t1 = new CallRunMethodTest();
      CallRunMethodTest t2 = new CallRunMethodTest();
      
      t1.run(); // run() called directly
      t2.run(); // run() called directly
   }
}

In the above example, two threads are created and the "run()" method is called directly on the threads rather than calling a "start()" method:

In the run() method: main
i: 0
i: 1
i: 2
i: 3
i: 4
In the run() method: main
i: 0
i: 1
i: 2
i: 3
i: 4

Example 2

The example below uses the start() method to run multiple threads, demonstrating the difference between the "run()" and "start()" methods:

public class RunVsStartExample extends Thread {
   public void run() {
       System.out.println("Running in thread: " + Thread.currentThread().getName());
   }
   
   public static void main(String[] args) {
       RunVsStartExample thread1 = new RunVsStartExample();
       RunVsStartExample thread2 = new RunVsStartExample();
   
       // Starts a new thread and runs start() in that thread
       thread1.start();
       thread2.start();
   }
}

The above program produces the following output:

Running in thread: Thread-0
Running in thread: Thread-1

The above two examples show the clear difference between using the run() and start() methods in threads. To execute code in the current thread, use the "run()" method; to achieve multithreading, use the "start()" method.

Updated on: 2025-05-14T12:15:29+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started