What are the 2 ways of multithreading in Java?

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, there are two primary ways to implement multithreading:

1. Extending the Thread Class

This is the simpler approach where you directly extend the Thread class and override its run() method. This is useful when you need to define a specific thread behavior by overriding the run() method.

Steps:

  1. Create a class that extends Thread.
  2. Override the run() method to define the task that the thread should perform.
  3. Create an instance of the class and call its start() method to initiate the thread.

Example:

class MyThread extends Thread { public void run() { System.out.println("Thread is running: " + Thread.currentThread().getName()); } } public class Main { public static void main(String[] args) { MyThread thread1 = new MyThread(); thread1.start(); // Start the thread MyThread thread2 = new MyThread(); thread2.start(); // Start another thread } }

In this approach:

  • The run() method contains the code that will be executed by the thread.
  • The start() method initiates the thread's execution, which calls the run() method.

2. Implementing the Runnable Interface

In this approach, instead of extending the Thread class, you implement the Runnable interface, which defines a run() method. This approach is preferred because it allows a class to extend another class while still being able to implement the Runnable interface. The Runnable interface separates the task from the thread, allowing better flexibility and code reuse.

Steps:

  1. Create a class that implements the Runnable interface.
  2. Implement the run() method to define the task.
  3. Pass the Runnable instance to a Thread object and call its start() method to initiate the thread.

Example:

class MyRunnable implements Runnable { public void run() { System.out.println("Runnable thread is running: " + Thread.currentThread().getName()); } } public class Main { public static void main(String[] args) { MyRunnable task = new MyRunnable(); Thread thread1 = new Thread(task); // Pass the Runnable to a Thread thread1.start(); // Start the thread Thread thread2 = new Thread(task); thread2.start(); // Start another thread } }

In this approach:

  • The run() method in the Runnable interface defines the task to be executed.
  • You create a Thread object by passing the Runnable implementation to it, and then call start() to begin execution.

Comparison Between the Two Approaches

FeatureExtending the Thread ClassImplementing the Runnable Interface
InheritanceCan only extend Thread, limiting other class extensions.Can implement Runnable and still extend another class.
FlexibilityLess flexible, as Java supports single inheritance.More flexible, as a class can implement multiple interfaces.
Multiple ThreadsEach thread is tied to a specific class that extends Thread.The same Runnable object can be passed to multiple threads.
Code ReusabilityLess reusable because of inheritance.More reusable, as the same Runnable can be used in different threads.
Resource SharingThreads share resources within the Thread class.Runnable can be shared between multiple threads, allowing for better resource management.

Conclusion

  • Extending the Thread class is simpler and works well when you don't need to inherit from other classes.
  • Implementing the Runnable interface is the preferred and more flexible approach, especially when you need to implement multiple interfaces or manage resources shared across threads.

To deepen your understanding of multithreading in Java and prepare for coding interviews, consider exploring these courses from DesignGurus.io:

These courses will help you build a solid foundation in multithreading and concurrency, preparing you for real-world applications and technical interviews.

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
Drawing from domain knowledge in industry-specific interview rounds
What is object-oriented programming? Explain OOP in depth.
Is MongoDB frontend or backend?
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.