What is the start() and run() method of thread class?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

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 an IllegalThreadStateException.
  • The start() method does not execute the code in run() 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 the run() method, but the actual execution of run() 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 the run() method in the current thread, which defeats the purpose of multithreading.
  • The run() method is a default method in the Thread 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 the run() method in the main thread (the thread from which the run() method was invoked).
  • This is not true multithreading because the run() method is not executed in a separate thread.

Summary of Differences:

MethodPurposeEffect
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 the run() method.
  • Do not call run() directly if you want to execute the code in a separate thread. Instead, use start() to initiate a new thread and automatically invoke run().

To understand multithreading and other concurrency-related topics in detail, and prepare for interviews, consider exploring the following courses from DesignGurus.io:

These courses will help you gain a deep understanding of multithreading and concurrency in Java, which is crucial for writing high-performance, scalable applications.

TAGS
Coding Interview
System Design Interview
CONTRIBUTOR
Design Gurus Team

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What is a Facebook behavioral interview?
What are the best coding interview preparation websites?
How do you manage a project?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.