What is difference between start and run method of Thread in Java? Answer (original) (raw)

Hello guys, what is the difference between calling start() vs run() method in Java multithreading is a popular core Java interview questions and if you are wondering the subtle difference between two then you have come to the right place. In this article, I will answer this question and show you an example as well. 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 the start() method internally calls the run() method of the 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 the 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 the run() method will be executed on a new thread, which is actually created by the start() method (see Java MasterClass course).

Another key difference between the start and run method to remember is that you can call the run method multiple times, JVM will not throw any error but when you cannot call the start() method on the 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 the following Java program to demonstrate to you the difference between calling the start() method on the thread and calling the run() method directly from the main thread.

Remember, in both 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 then it will be called on the same thread i.e. main thread in our case.

Don't believe it? run the code and see by yourself. Let's see the example now.

/**

}

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 then a new thread with the 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.

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 book by Scott Oaks, which explains every key multi-threading concept and thread basics in good detail.

Difference between start and run method of Thread in Java

That's all about the difference between the start() and run() methods in Java. Just remember that even though the start() method internally calls the run() method, its main purpose is to create a new thread. If you directly call the run() method then 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

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