Difference between Thread.start() and Thread.run() method in Java?

If you remember, a Thread is started in Java by calling the start() method of java.lang.Thread class, but if you learn more you will find out that start() method internally calls the run() method of Runnable interface to execute the code specified in the run() method in the separate thread. Now the question comes, why can't you just call the run() method instead of calling the start() method because anyway start() is calling the run()? This is one of the tricky multi-threading question you will find on Java interviews. The trick here is that, when you directly call the run() method than the code inside run() method will not be executed on a new thread, instead it will be executed on the same thread. On the other hand, when you call the Thread.start() method, then the code inside run() method will be executed on a new thread, which is actually created by the start() method. This is one of the fundamental of threading in Java, which is often get overlooked by Java developers unless you have read book like Java Threads By Scott Oaks, which explains every key multi-threading concept and thread basics in good detail.

Difference between start and run method in Java thread


Difference between start() and run() method of Thread class

The run() method comes form the Runnable interface but the start() method is only declared in the Thread class. Since java.lang.Thread class implements Runnable interface, you can access the run() method from an instance of Thread class. I have create following Java program to demonstrate you the difference of calling start() method on thread and calling the run() method directly from the main thread.


Remember, in both the cases the run() method will be called but when you call the start() method than it will be called by a new thread. On the other hand if you call the run() method directly than it will be called on the same thread i.e. main thread in our case. Don't believe? run the code and see by yourself.  Let's see the example now.

/**
 * Java Program to demonstrate the difference between run() vs start() method of
 * Thread class in Java. Just remember that when you directly call the run()
 * method, code written inside the run() method will be executed in the calling
 * thread, but when you call the start() method then a new thread will be
 * created to execute the code written inside run() method in Java.
 * 
 * @author WINDOWS 8
 *
 */
public class HelloWorldApp {

    public static void main(String args[]) {
        Thread t = new Thread() {

            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()
                        + " is executed the run() method");
            }
        };

        System.out.println( Thread.currentThread().getName() + " Calling the start() method of Thread");
        t.start();
        
        // let's wait until the thread completes execution
        try {
            t.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        System.out.println( Thread.currentThread().getName() + " Calling the run() method of Thread");
        t.run();
    }
}

and here is the output:

main Calling the start() method of Thread
Thread-0 is executed the run() method
main Calling the run() method of Thread
main is executed the run() method

You can clearly see that when our main thread (the thread which executes the main() method in Java) calls the start() method than a new thread with name Thread-0 is created and run() method is executed by that thread, but if you directly call the run() method than its executed on the same main thread in Java. 


That's all about difference between start() and run() method in Java. Just remember that even though start() method internally calls the run() method, its main purpose is to create a new thread. If you directly call the run() method than a new thread will not be created instead run() will get executed on the same thread. It means, you should always start the thread by calling Thread.start() method in Java.

Related Java multi-threading Interview Questions from Java
  • What is difference between the yield() and sleep() method in Java? (answer)
  • 5 differences between wait() and sleep() method in Java? (answer)
  • How to join multiple threads in Java? (answer)
  • How to stop a thread in Java? (answer)
  • How to pause a thread in Java? (answer)
  • Difference between wait() and yield() method in Java? (answer)
  • Difference between CyclicBarrier and CountDownLatch in Java? (answer)
  • Difference between notify() and notifyAll() in Java? (answer)


Recommended books to master Java Multi-threading and Concurrency
  1. Java Threads By Scott Oaks and Henry Wong
  2. Java Concurrency in Practice by Brian Goetz

No comments:

Post a Comment