Creating Threads by Extending the Thread Class(continues)
Table of Contents
- Introduction
- Understanding Threads in Java
- Creating Threads by Extending the Thread Class
- Example Implementation of a Thread
- Conclusion
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
- Create a class that extends the
Thread
class. - Override the
run()
method in the new class. - Start the thread using the
start()
method.
Here’s the syntax for creating a thread:
1 2 3 4 5 6 7 8 9 |
class MyThread extends Thread { @Override public void run() { // Thread logic } } MyThread thread = new MyThread(); thread.start(); |
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
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
package org.studyeasy; import static java.lang.Thread.sleep; class MyCounter extends Thread { private int threadNo; public MyCounter(int threadNo) { this.threadNo = threadNo; } @Override public void run() { try { countMe(); } catch (InterruptedException e) { e.printStackTrace(); } } public void countMe() throws InterruptedException { for (int i = 0; i < 10; i++) { sleep(500); System.out.println("Thread no: " + threadNo + " and iteration no: " + i); } } } public class Main { public static void main(String[] args) throws InterruptedException { MyCounter counter1 = new MyCounter(1); MyCounter counter2 = new MyCounter(2); MyCounter counter3 = new MyCounter(3); long startTime = System.currentTimeMillis(); counter1.start(); System.out.println("********************************************"); counter2.start(); counter3.start(); sleep(5100); long endTime = System.currentTimeMillis(); System.out.println("Total time required for processing: " + (endTime - startTime)); } } |
Explanation of the Code
- Class
MyCounter
: This class extendsThread
. 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 ofMyCounter
, each representing a separate thread. The threads are started using thestart()
method, which initiates the execution of therun()
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.
1 2 3 4 5 6 7 |
******************************************** Thread no: 1 and iteration no: 0 Thread no: 2 and iteration no: 0 Thread no: 1 and iteration no: 1 Thread no: 3 and iteration no: 0 ... Total time required for processing: 5100ms |
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.