S12L02 – Creating thread by extending the thread class

Creating a Thread by Extending the Thread Class

Table of Contents

1. Introduction

In Java, multi-threading is a critical concept that allows multiple operations to run concurrently, improving application performance.
In this article, we will explore how to create threads by extending the Thread class, which is one of the fundamental approaches
to handle multi-threading in Java.

Java provides two primary methods to create a thread:

  • By extending the Thread class
  • By implementing the Runnable interface

In this article, we will focus on creating threads by extending the Thread class. We will walk through a practical example to
show how this method works, explore its pros and cons, and explain the importance of using threads in multi-tasking operations.

2. Understanding Threads in Java

What is a Thread?

A thread in Java is a lightweight process that runs independently and can execute parallel tasks. Multiple threads can exist within the same process,
sharing resources but executing independently. This allows Java applications to perform multiple operations concurrently, making them more efficient
in handling tasks like I/O operations, background tasks, etc.

When to Use Threads

  • Tasks that take a long time to execute: Threads allow other tasks to continue without waiting for one task to complete.
  • Concurrency and parallelism: Threads improve performance by enabling simultaneous operations.
  • Background tasks: Tasks that do not require user interaction, such as sending emails, processing files, or performing calculations in the background.

3. Creating a Thread by Extending the Thread Class

Overview

To create a thread by extending the Thread class in Java, you need to define a new class that extends Thread.
You must then override the run() method, which contains the code that will be executed when the thread runs.
Once the thread object is created, you can invoke the start() method to run the thread.

Code Structure

4. Code Explanation and Implementation

Explanation of the Main Code

  • Extending the Thread Class: We created a class MyThread that extends the Thread class. The thread number is passed to the constructor, which will be used to distinguish between different threads.
  • The run() Method: Inside the run() method, we implement the logic of what each thread will do. In this case, the thread prints its number and the iteration count for 10 iterations. The Thread.sleep(500) pauses the thread for 500 milliseconds between each iteration to simulate some work being done.
  • Creating Threads: In the Main class, two threads are created (thread1 and thread2). Each thread has a unique identifier (1 and 2).
  • Starting Threads: The start() method begins the execution of each thread in parallel. This method internally calls the run() method of each thread.

Output

This shows both threads executing their work in parallel, with intermittent delays.

5. Conclusion

In this article, we explored how to create threads in Java by extending the Thread class. Threads are useful for performing multiple tasks simultaneously and improving application performance.
This method is straightforward but can be limiting if you want to use inheritance for another purpose, which is why implementing Runnable is often preferred for complex use cases.

Key Takeaways:

  • Multi-threading improves the performance of applications by running tasks concurrently.
  • Extending the Thread class is a simple way to create threads but has some limitations when multiple inheritance is involved.
  • Use threads when you want to handle long-running tasks or perform background operations without interrupting the main application flow.