What is a Thread in Java?
A thread is a lightweight subprocess, the smallest unit of processing. Java supports multithreading, which means you can run multiple threads concurrently — increasing the efficiency and performance of your program.
Why Use Threads?
- Perform tasks in parallel (e.g., downloading, file I/O, UI responsiveness).
- Improve application speed and responsiveness.
- Handle background tasks (like animations, network calls).
Ways to Create a Thread in Java
- By extending the
Thread
class - By implementing the
Runnable
interface - (Java 8+) Using Lambda Expressions
Extending Thread
Class
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running using Thread class.");
}
}
public class ThreadExample1 {
public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.start(); // starts the thread and calls run()
}
}
Don't use
.run()
directly; use.start()
to create a new thread.
Implementing Runnable
Interface
class MyRunnable implements Runnable {
public void run() {
System.out.println("Thread is running using Runnable interface.");
}
}
public class ThreadExample2 {
public static void main(String[] args) {
Thread t1 = new Thread(new MyRunnable());
t1.start();
}
}
This approach is better when your class needs to extend another class, as Java doesn't support multiple inheritance.
Using Lambda Expression (Java 8+)
public class LambdaThreadExample {
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
System.out.println("Thread is running using Lambda.");
});
t1.start();
}
}
Example: Running Multiple Threads
class Task1 extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Task1 - " + i);
}
}
}
class Task2 extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Task2 - " + i);
}
}
}
public class MultiThreadDemo {
public static void main(String[] args) {
Task1 t1 = new Task1();
Task2 t2 = new Task2();
t1.start();
t2.start();
}
}
Output (order may vary):
Task1 - 1
Task2 - 1
Task1 - 2
Task2 - 2
...
Threads run asynchronously, so output order may change every time.
Thread Methods
Method | Description |
---|---|
start() |
Starts the thread |
run() |
Entry point for thread logic |
sleep(ms) |
Pauses the thread for given time |
join() |
Waits for a thread to die |
isAlive() |
Checks if thread is still running |
setPriority() |
Sets thread priority (1 to 10) |
Thread Synchronization
When multiple threads access shared resources (e.g., a bank account), you must use synchronization to prevent data inconsistency.
class Counter {
int count = 0;
synchronized void increment() {
count++;
}
}
public class SyncExample {
public static void main(String[] args) throws InterruptedException {
Counter counter = new Counter();
Thread t1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) counter.increment();
});
Thread t2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) counter.increment();
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("Final Count: " + counter.count);
}
}
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.