S12L03 – Creating thread by extending the thread class continues

Creating Threads by Extending the Thread Class(continues)

Table of Contents

Introduction

In this article, we will explore how to create and manage threads in Java by extending the Thread class. Multithreading is a powerful feature in Java that allows the execution of multiple threads simultaneously, improving the efficiency and responsiveness of applications. By the end of this article, you will have a clear understanding of thread creation using the Thread class, its advantages, and key considerations.

Understanding Threads in Java

A thread is a lightweight process that runs concurrently with other threads within a program. In Java, threads can be created in two primary ways:

  • Extending the Thread class
  • Implementing the Runnable interface

In this article, we will focus on the first approach: extending the Thread class. This method is straightforward and commonly used when the class already extends Thread.

Advantages of Multithreading

  • Efficiency: Multiple threads can perform tasks simultaneously, reducing overall execution time.
  • Resource Sharing: Threads within the same process share resources, which can reduce memory overhead.
  • Responsiveness: Multithreading improves the responsiveness of applications, especially in user interfaces.

Drawbacks

  • Complexity: Managing multiple threads can become complex, especially when synchronization is required.
  • Resource Contention: Threads may compete for limited resources, leading to performance bottlenecks.

Creating Threads by Extending the Thread Class

In Java, you can create a thread by extending the Thread class and overriding its run() method, where the logic of the thread is defined.

Steps to Create a Thread

  1. Create a class that extends the Thread class.
  2. Override the run() method in the new class.
  3. Start the thread using the start() method.

Here’s the syntax for creating a thread:

When to Use This Approach

You should extend the Thread class when:

  • You are creating a specialized thread that doesn’t need to share the same behavior as other threads.
  • Your class already extends another class, and multithreading is just one of its capabilities.

Example Implementation of a Thread

Let’s explore an example to better understand how to create and manage threads using this approach.

Java Code Example

Explanation of the Code

  • Class MyCounter: This class extends Thread. It represents a thread that performs a counting operation. Each thread instance has a unique number (threadNo).
  • countMe() method: This method simulates a delay of 500 milliseconds per iteration and prints the current thread number and iteration count.
  • Main Class: In the main() method, we create three instances of MyCounter, each representing a separate thread. The threads are started using the start() method, which initiates the execution of the run() method in each thread.

Output of the Code

The program will output the thread number and iteration number for each thread, running concurrently. After all threads have finished execution, the total time taken is displayed.

Conclusion

In this article, we discussed how to create threads in Java by extending the Thread class. Multithreading can greatly improve the performance of applications by allowing multiple tasks to run simultaneously. The example provided demonstrates how to create, start, and manage multiple threads. Understanding how to work with threads is essential for building responsive and efficient Java applications.