Creating a Thread by Extending the Thread Class
Table of Contents
- Introduction
- Understanding Threads in Java
- Creating a Thread by Extending the Thread Class
- Code Explanation and Implementation
- Conclusion
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
class MyThread extends Thread { private int threadNo; public MyThread(int threadNo) { this.threadNo = threadNo; } @Override public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep(500); // Simulates work System.out.println("Thread no: " + threadNo + ", iteration: " + i); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Main { public static void main(String[] args) { MyThread thread1 = new MyThread(1); MyThread thread2 = new MyThread(2); thread1.start(); thread2.start(); } } |
4. Code Explanation and Implementation
Explanation of the Main Code
- Extending the
Thread
Class: We created a classMyThread
that extends theThread
class. The thread number is passed to the constructor, which will be used to distinguish between different threads. - The
run()
Method: Inside therun()
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. TheThread.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
andthread2
). 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 therun()
method of each thread.
Output
1 2 3 4 5 6 7 8 |
Thread no: 1, iteration: 0 Thread no: 1, iteration: 1 Thread no: 1, iteration: 2 ... Thread no: 2, iteration: 0 Thread no: 2, iteration: 1 Thread no: 2, iteration: 2 ... |
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.