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.
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, If you like courses better than books then you can also check out these best Java Multithreading and concurrency courses to learn more about the basics of threads in Java.
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.
/** * 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 java67 * */ 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 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.
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.
And, If you like courses better than books then you can also check out these best Java Multithreading and concurrency courses to learn more about the basics of threads 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.
Related Java multi-threading Interview Questions from Java
- What is the difference between the yield() and sleep() methods in Java? (answer)
- 5 differences between the 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
- How to avoid deadlock in Java? (answer)
- Understanding the flow of data and code in Java program (answer)
- How to do inter-thread communication in Java using wait-notify? (answer)
- 5 Courses to Learn Java Multithreading in-depth (courses)
- Difference between atomic, volatile, and synchronized in Java? (answer)
- 10 Advanced books for Experienced Programmers (books)
- 50+ Thread Interview Questions for Beginners (questions)
- What is happens before in Java Concurrency? (answer)
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.
No comments:
Post a Comment
Feel free to comment, ask questions if you have any doubt.