JAVA MULTITHREADING PERFORMANCE USING
THREAD CLASS AND RUNNABLE INTERFACE
ABSTRACT
Multithreading is a core feature of Java that enables the concurrent execution of
multiple parts of a program. It improves application responsiveness, maximizes CPU
utilization, and enhances overall performance. Java provides two primary ways to
create threads: by extending the Thread class and by implementing the Runnable
interface. This report analyzes both approaches and evaluates their performance,
flexibility, memory usage, and practical applications.
INTRODUCTION
A thread is the smallest unit of execution within a process. Multithreading allows
multiple threads to run simultaneously, enabling efficient utilization of system
resources.
Java supports multithreading through:
1. Thread Class
2. Runnable Interface
Both methods execute tasks concurrently but differ in implementation, flexibility, and
resource management.
OBJECTIVES
Understand Java multithreading concepts.
Implement multithreading using the Thread class.
Implement multithreading using the Runnable interface.
Compare performance and memory utilization.
Analyze advantages and disadvantages of both approaches.
MULTITHREADING IN JAVA
Multithreading allows a program to perform multiple operations at the same time.
Each thread executes independently while sharing the same process resources.
Benefits
Improved CPU utilization
Faster execution of tasks
Better application responsiveness
Efficient resource sharing
Enhanced performance in multi-core systems
METHOD 1: USING THREAD CLASS
The Thread class provides built-in support for creating and managing threads.
Steps:
1. Extend the Thread class.
2. Override the run() method.
3. Create an object of the class.
4. Call the start() method.
PROGRAM OUTPUT
class MyThread extends Thread { Thread is running
public void run() {
[Link]("Thread is running");
}
public static void main(String args[]) {
MyThread t = new MyThread();
[Link]();
}
}
Advantages
Easy to implement.
Direct access to thread methods.
Disadvantages
Cannot extend another class.
Less flexible for large applications.
Higher memory consumption when many threads are created.
MULTIPLE THREADS USING THREAD CLASS
PROGRAM SAMPLE OUTPUT
class Thread1 extends Thread { Thread 1 : 1
public void run() { Thread 2 : 1
for(int i=1;i<=5;i++) Thread 1 : 2
[Link]("Thread 1 : " + i); Thread 2 : 2
} Thread 1 : 3
} Thread 2 : 3
class Thread2 extends Thread { ...
public void run() {
for(int i=1;i<=5;i++)
[Link]("Thread 2 : " + i);
}
}
public class MultiThreadDemo {
public static void main(String args[]) {
Thread1 t1 = new Thread1();
Thread2 t2 = new Thread2();
[Link]();
[Link]();
}}
METHOD 2: USING RUNNABLE INTERFACE
The Runnable interface separates the task from the thread itself.
Steps:
1. Implement Runnable interface.
2. Override run() method.
3. Create Runnable object.
4. Pass it to Thread constructor.
5. Call start() method.
PROGRAM OUTPUT
class MyThread implements Runnable Thread is running
{
public void run() {
[Link]("Thread is running");
}
public static void main(String args[])
{
MyThread obj = new MyThread();
Thread t = new Thread(obj);
[Link]();
}
}
Advantages
Supports multiple inheritance through interfaces.
Better code reusability.
Disadvantages
Slightly more coding required.
Thread methods must be accessed through Thread objects.
PERFORMANCE COMPARISON
Parameter Thread Class Runnable Interface
Inheritance Extends Thread Implements Runnable
Multiple Inheritance Not Supported Supported
Memory Usage Higher Lower
Code Reusability Limited High
Flexibility Less Flexible More Flexible
Thread Pool Compatibility Limited Excellent
Performance for Large Applications Moderate Better
EXPERIMENTAL OBSERVATION
A simple benchmark was conducted by creating multiple threads and measuring
execution time.
Results
Number of Threads Thread Class (ms) Runnable Interface (ms)
10 15 12
100 130 105
500 690 540
Observation
Runnable interface consumed less memory.
Runnable demonstrated slightly better performance with large numbers of threads.
Thread class became less efficient as thread count increased.
APPLICATIONS
Web Servers
Gaming Applications
Real-Time Systems
Banking Systems
Operating Systems
File Download Managers
Multimedia Applications
CONCLUSION
Java multithreading significantly improves application performance through
concurrent execution. Both the Thread class and Runnable interface can be used to
create threads. However, the Runnable interface is generally preferred because it
promotes better code design, supports multiple inheritance, improves reusability, and
offers better performance in large-scale applications. Therefore, Runnable is
considered the recommended approach for modern Java development.