How many types of multithreading are there in Java?
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 aThread
object and call itsstart()
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 multipleThread
objects.
Cons:
- Slightly more complex than extending
Thread
for simple tasks.
Key Differences Between the Two Approaches:
Aspect | Extending Thread | Implementing Runnable |
---|---|---|
Inheritance | Limits you to extending only one class. | More flexible, as you can implement multiple interfaces. |
Code Reusability | Less reusable. You cannot pass the same task to multiple threads. | More reusable; the same Runnable task can be passed to multiple threads. |
Separation of Concerns | The task and thread management are coupled. | The task is separate from thread management, leading to better separation of concerns. |
Flexibility | Less 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:
- Extending the
Thread
class: Suitable for simpler scenarios where you don’t need to inherit from other classes. - Implementing the
Runnable
interface: More flexible and preferred for complex systems, as it allows better code reuse and separation of concerns.
Recommended Courses:
To deepen your understanding of multithreading and concurrency in Java, consider enrolling in 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 provide a comprehensive understanding of multithreading and concurrency, helping you build efficient, thread-safe applications in Java.
GET YOUR FREE
Coding Questions Catalog