What is the start() and run() method of thread class?
In Java, the Thread
class provides two crucial methods for starting and managing threads: start()
and run()
. Understanding the difference between these two methods is key to working with multithreading in Java.
1. start()
Method
The start()
method is used to begin the execution of a thread. When you invoke start()
, it creates a new thread of execution and calls the run()
method internally on that thread. It doesn't directly execute the code in the run()
method in the calling thread; instead, it schedules the run()
method to be executed by a separate thread.
Key Points:
- The
start()
method initiates the thread, causing it to transition from the new state to the runnable state. - The
start()
method can only be called once per thread. If called multiple times on the same thread, it will throw anIllegalThreadStateException
. - The
start()
method does not execute the code inrun()
immediately; instead, it schedules it for execution by the JVM's thread scheduler.
Example:
class MyThread extends Thread { public void run() { System.out.println("Thread is running"); } public static void main(String[] args) { MyThread thread1 = new MyThread(); thread1.start(); // This calls the start() method, which will invoke run() internally } }
In this example:
- When
thread1.start()
is called, it triggers therun()
method, but the actual execution ofrun()
happens in a separate thread managed by the JVM.
2. run()
Method
The run()
method is where the actual code of the thread resides. It contains the task that the thread will execute when it is started. This method is not executed directly by calling run()
; rather, it is called by the start()
method when the thread is initiated.
Key Points:
- The
run()
method contains the code that should be executed by the thread. - Directly calling
run()
does not start a new thread; it simply executes therun()
method in the current thread, which defeats the purpose of multithreading. - The
run()
method is a default method in theThread
class that can be overridden to define specific thread behavior.
Example:
class MyThread extends Thread { public void run() { System.out.println("Thread is running"); } public static void main(String[] args) { MyThread thread1 = new MyThread(); thread1.run(); // This calls run() directly, but doesn't create a new thread } }
In this example:
- Calling
thread1.run()
does not create a new thread. Instead, it runs therun()
method in the main thread (the thread from which therun()
method was invoked). - This is not true multithreading because the
run()
method is not executed in a separate thread.
Summary of Differences:
Method | Purpose | Effect |
---|---|---|
start() | Begins the execution of a thread. It invokes the run() method internally in a new thread. | Starts a new thread of execution. This method can only be called once per thread. |
run() | Contains the code to be executed by the thread. | Executes the code in the current thread. Calling run() directly does not start a new thread. |
Key Takeaways:
- Use
start()
to start a new thread of execution and call therun()
method. - Do not call
run()
directly if you want to execute the code in a separate thread. Instead, usestart()
to initiate a new thread and automatically invokerun()
.
Recommended Courses
To understand multithreading and other concurrency-related topics in detail, and prepare for interviews, consider exploring the following courses from DesignGurus.io:
- Grokking Multithreading and Concurrency for Coding Interviews
- Grokking Data Structures & Algorithms for Coding Interviews
- Grokking the System Design Interview
These courses will help you gain a deep understanding of multithreading and concurrency in Java, which is crucial for writing high-performance, scalable applications.
GET YOUR FREE
Coding Questions Catalog