Difference between thread start() and run()


Difference Thread start run

Summary: It is a frequently asked question to me by beginners of Java. What happens if we call directly run() instead of start() on the thread. Explained clearly on simple terms and code in this tutorial "Difference Thread start run".

First let us see the two codes of calling start() and run() methods with screenshots. The observation of screenshots itself tells the difference and what to be used and what not to be used.

Normal code calling start() method on the thread
public class Demo extends Thread
{
  public void run()           
  {
    System.out.println("Thread Status alive: " + this.isAlive());
  }      
  public static void main(String args[])
  {
     Demo d1 = new Demo();
     d1.start();
  }
}

Difference Thread start run

Abnormal code calling run() method on the thread
public class Demo extends Thread
{
  public void run()           
  {
    System.out.println("Thread Status alive: " + this.isAlive());
  }      
  public static void main(String args[])
  {
     Demo d1 = new Demo();
     d1.run();
  }
}

Difference Thread start run

Observe, both the screenshots. With start() method, the thread isAlive() method returns true and with run() method, the thread isAlive() returns false. What does it indicates; in both the styles, thread is created and run() called. With start() style, the JVM implicitly calls run() method in run() method, the thread d1 being executed. For this isAlive() returned true. If start() is called, the JVM maintains the life cycle of thread and does many activities on the thread, thread is eligible.

By calling run() method directly as d1.run(), as in the second case, it is equal to simply a normal method call. in d1.run(), d1 is assumed, by JVM, as an object of Demo class but not thread of Demo class. That is, run() is not called or executed by a thread. For this reason, isAlive() returned false.

So use always d1.start() and not d1.run().

Salient points to understand better the concept

  1. If start() is called, the thread is activated and run() method is executed in the context of this thread. For this reason, the first screenshot gives true. If run() is called directly, the run() is executed in the context of some different current thread, may be of JVM like main thread. As different thread is executing run(), the second screenshot gives false.
  2. start() method in Java is declared native having implemented natively. That is, start() method calls and communicates with underlying OS thread management system. Following is the signature of start method.

    public native synchronized void start();

    The JVM assigns the responsibility of calling run() method to thread scheduler entirely managed by OS. The scheduler calls the run() method at its discretion as per the priority, waiting time and availability of processor time etc.

  3. Calling run() does not create or go with a new thread.
  4. Invoking run() directly is just calling a method but not starting a thread.

Leave a Comment

Your email address will not be published.