Introduction
Multithreading is one of the most powerful features of Java. It allows multiple tasks to execute simultaneously, improving the performance and responsiveness of applications. Whether you are building desktop software, web applications, games, or enterprise systems, threads play a vital role in handling multiple operations efficiently.
Java provides several ways to create threads, but one of the most common approaches is by extending the Thread class. The Thread class is part of the java.lang package and contains built-in methods for creating, controlling, and managing threads.
Understanding the Java Thread class is essential for every Java developer because it forms the foundation of concurrent programming. Although modern Java often prefers the Runnable interface or the ExecutorService framework, learning the Thread class helps you understand how Java threads work internally.
What is Java Thread Class?
The Java Thread class is a predefined class in the java.lang package that allows developers to create and manage threads.
A thread represents a separate path of execution inside a program. Every Java application starts with one thread known as the Main Thread. Additional threads can be created using the Thread class.
When you extend the Thread class, you override its run() method. The code inside the run() method executes in a separate thread after calling the start() method.
Key Features
- Part of the java.lang package
- Used to create new threads
- Supports concurrent execution
- Contains several thread management methods
- Helps improve application performance
- Supports thread lifecycle management
Thread Class Hierarchy
Object
|
Thread
The Thread class directly extends the Object class.
Syntax
The basic syntax for creating a thread by extending the Thread class is shown below.
class MyThread extends Thread {
public void run() {
// Code executed by thread
}
}
public class Main {
public static void main(String[] args) {
MyThread t = new MyThread();
t.start();
}
}
How Thread Class Works
The process consists of four simple steps:
- Create a class that extends Thread.
- Override the run() method.
- Create an object of the thread.
- Call the start() method.
Example:
Create Thread
↓
Override run()
↓
Create Object
↓
Call start()
↓
run() executes
Example 1: Basic Thread Class Example
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running...");
}
}
public class Main {
public static void main(String[] args) {
MyThread t = new MyThread();
t.start();
}
}
Output
Why Use start() Instead of run()?
Many beginners directly call the run() method.
Incorrect:
MyThread t = new MyThread();
t.run();
Correct:
MyThread t = new MyThread();
t.start();
Difference
| run() | start() |
|---|---|
| Executes like a normal method | Creates a new thread |
| No multithreading | Multithreading begins |
| Runs in current thread | Runs in separate thread |
Example 2: Multiple Threads
class MyThread extends Thread {
public void run() {
for(int i=1;i<=5;i++) {
System.out.println(getName() + " : " + i);
}
}
}
public class Main {
public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
t1.start();
t2.start();
}
}
Sample Output
Thread-1 : 1
Thread-0 : 2
Thread-1 : 2
…
Both threads execute independently.
Common Methods of Thread Class
The Thread class provides many useful methods.
| Method | Description |
|---|---|
| start() | Starts a new thread |
| run() | Contains thread code |
| sleep() | Pauses thread execution |
| join() | Waits for another thread |
| currentThread() | Returns current thread |
| getName() | Returns thread name |
| setName() | Changes thread name |
| getPriority() | Returns thread priority |
| setPriority() | Sets thread priority |
| isAlive() | Checks whether thread is running |
Example: Naming a Thread
class MyThread extends Thread {
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
public class Main {
public static void main(String[] args) {
MyThread t = new MyThread();
t.setName("Download Thread");
t.start();
}
}
Output
Example: Thread Sleep
class MyThread extends Thread {
public void run() {
try {
for(int i=1;i<=5;i++) {
System.out.println(i);
Thread.sleep("111");
}
}
catch(Exception e) {
System.out.println(e);
}
}
}
public class Main {
public static void main(String[] args) {
MyThread t = new MyThread();
t.start();
}
}
Output
2
3
4
5
Each number appears after one second.
Thread Lifecycle
A Java thread passes through different states during execution.
New
↓
Runnable
↓
Running
↓
Blocked / Waiting
↓
Runnable
↓
Terminated
States Explained
New
The thread object is created but not started.
Runnable
The thread is ready for execution.
Running
The CPU executes the thread.
Waiting/Blocked
The thread temporarily waits.
Terminated
The thread completes execution.
Real-life Example
Imagine a banking application.
A user performs multiple operations simultaneously:
- Checking account balance
- Downloading statement
- Sending money
- Receiving notifications
- Updating transaction history
Instead of waiting for one task to finish before starting another, Java creates separate threads for each operation.
Main Program
|
-------------------------------
| | | | |
Balance Transfer SMS Statement Update
This makes the banking application fast and responsive.
Thread Class vs Runnable Interface
| Thread Class | Runnable Interface |
|---|---|
| Class | Interface |
| Uses inheritance | Uses implementation |
| Cannot extend another class | Can extend another class |
| Suitable for simple programs | Preferred for large applications |
| Less flexible | More flexible |
Advantages of Thread Class
- Easy to understand
- Simple thread creation
- Built-in thread management
- Supports concurrent programming
- Improves application responsiveness
- Useful for beginners
Disadvantages
- Java supports single inheritance.
- Extending Thread prevents extending another class.
- Not recommended for complex applications.
- Runnable is generally preferred in professional development.
Best Practices
- Always call start() instead of run().
- Keep the run() method lightweight.
- Handle exceptions properly.
- Avoid unnecessary thread creation.
- Use thread synchronization when sharing resources.
- Prefer ExecutorService for large-scale applications.
Common Mistakes
1. Calling run() Directly
Incorrect
t.run();
Correct
t.start();
2. Forgetting to Override run()
class MyThread extends Thread {
}
Nothing executes because the default run() method is empty.
3. Creating Too Many Threads
Creating hundreds of unnecessary threads may reduce performance instead of improving it.
4. Ignoring Exceptions
Always handle exceptions when using Thread.sleep().
try {
Thread.sleep("111");
}
catch(Exception e) {
}
5. Assuming Threads Execute in Order
Never assume:
Thread A
Thread B
The JVM scheduler decides the execution order.
Conclusion
The Java Thread class provides a simple and straightforward way to create threads and perform concurrent programming. By extending the Thread class and overriding the run() method, you can execute tasks independently from the main program, making applications faster and more responsive.
The Thread class also offers useful methods such as start(), sleep(), join(), interrupt(), setName(), and isAlive() for managing thread execution. While extending the Thread class is excellent for learning and small applications, larger projects typically favor the Runnable interface or the ExecutorService framework due to their greater flexibility and support for better object-oriented design.