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 a separate thread. Now the question comes, why can't you just call the run() method instead of calling the start() method? Since start() is calling the run() anyway, calling run directly should have the same effect as calling the start, no?

Well, this is one of the tricky multi-threading questions you will find on Java interviews and it's not easy as it looks, especially if you have half knowledge about threads in Java.

The trick here is that when you directly call the run() method then the code inside run() method is executed in the same thread which calls the run method. JVM will not create a new thread until you call the start method.

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 (see Java Fundamentals: The Java Language).

Another key difference between start and run method to remember is that you can call the run method multiple time, JVM will not throw any error but when you cannot call the start() method on same thread instance.

The first time, t.start() will create a new thread but the second time it will throw java.lang.IllegalStateException, because the thread is already started and you cannot restart it again, you can only pause a thread in Java. Once it died it's gone.



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

The run() method comes from 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 created following Java program to demonstrate you the difference of calling start() method on the 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 then 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.

Difference between start and run method of Thread in Java


This is one of the fundamentals of threading in Java, which is often get overlooked by Java developers unless you have read a 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

If you like courses better than books then you can also check out Java Fundamentals Part 1,2, a free course from Pluralsight to learn more about basics of threads in Java.


That's all about the 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
  1. What is the difference between the yield() and sleep() method in Java? (answer)
  2. 5 differences between wait() and sleep() method in Java? (answer)
  3. How to join multiple threads in Java? (answer)
  4. How to stop a thread in Java? (answer)
  5. How to pause a thread in Java? (answer)
  6. Difference between wait() and yield() method in Java? (answer)
  7. Difference between CyclicBarrier and CountDownLatch in Java? (answer)
  8. Difference between notify() and notifyAll() in Java? (answer)

Further Learning
Java Fundamentals Part 1,2
Java Concurrency in Practice
Applying Concurrency and Multi-threading to Common Java Patterns

Thanks for reading this article, if you like this tutorial then please share with your friends and colleagues. If you have any questions or feedback then please drop a comment. 

No comments:

Post a Comment