How many types of multithreading are there 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, multithreading can be implemented in two primary ways:

1. Extending the Thread Class

This is one of the simplest ways to implement multithreading in Java. You create a new class that extends the Thread class and override its run() method, which contains the code that will be executed in the new thread.

Steps:

  • Extend the Thread class.
  • Override the run() method to define the task that the thread will perform.
  • Call start() to begin the thread’s execution.

Example:

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

Pros:

  • Simple and straightforward for basic thread creation.

Cons:

  • You can only extend one class in Java, so you can't extend any other class if you are extending the Thread class. It reduces flexibility in some situations.

2. Implementing the Runnable Interface

This is a more flexible and preferred method of creating threads in Java. Instead of extending Thread, you create a class that implements the Runnable interface and override its run() method. Then, you pass the Runnable object to a Thread object, which executes the run() method.

Steps:

  • Implement the Runnable interface.
  • Override the run() method to define the task that the thread will perform.
  • Pass the Runnable object to a Thread object and call its start() method.

Example:

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

Pros:

  • More flexible because you can implement multiple interfaces, and it allows you to extend other classes.
  • Better code reuse, as the Runnable object can be passed to multiple Thread objects.

Cons:

  • Slightly more complex than extending Thread for simple tasks.

Key Differences Between the Two Approaches:

AspectExtending ThreadImplementing Runnable
InheritanceLimits you to extending only one class.More flexible, as you can implement multiple interfaces.
Code ReusabilityLess reusable. You cannot pass the same task to multiple threads.More reusable; the same Runnable task can be passed to multiple threads.
Separation of ConcernsThe task and thread management are coupled.The task is separate from thread management, leading to better separation of concerns.
FlexibilityLess flexible, as you can only extend Thread.More flexible, as you can implement other interfaces while implementing Runnable.

Conclusion:

There are two main types of multithreading in Java:

  1. Extending the Thread class: Suitable for simpler scenarios where you don’t need to inherit from other classes.
  2. Implementing the Runnable interface: More flexible and preferred for complex systems, as it allows better code reuse and separation of concerns.

To deepen your understanding of multithreading and concurrency in Java, consider enrolling in the following courses from DesignGurus.io:

These courses provide a comprehensive understanding of multithreading and concurrency, helping you build efficient, thread-safe applications in Java.

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
Why learn System Design?
What are the common interview questions?
What to wear for a Microsoft interview?
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.