Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum CPU utilization. This repository covers everything from thread basics to advanced synchronization techniques.
- ⚡ Performance - Utilize multiple CPU cores
- 🔄 Responsiveness - Keep UI responsive during heavy tasks
- 📊 Efficiency - Better resource utilization
- 🎯 Parallelism - Execute multiple tasks simultaneously
| Concept | Description |
|---|---|
| Thread | Lightweight process, smallest unit of execution |
| Runnable | Interface for creating threads |
| Synchronized | Prevent race conditions |
| Deadlock | Circular wait condition |
| Thread Pool | Reusable thread collection |
| Executor | Framework for thread management |
┌─────────────────────────────────────────┐
│ │
▼ │
┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ NEW │───►│ RUNNABLE │───►│ RUNNING │───►│TERMINATED│ │
└──────────┘ └──────────┘ └────┬─────┘ └──────────┘ │
│ ▲ │ │
│ │ ▼ │
│ │ ┌──────────┐ │
│ └─────────│ BLOCKED/ │ │
│ │ WAITING │ │
│ └──────────┘ │
│ │
└────────────────────────────────────────────────────────┘
// Method 1: Extending Thread class
class MyThread extends Thread {
public void run() {
System.out.println("Thread running: " + getName());
}
}
// Method 2: Implementing Runnable
class MyRunnable implements Runnable {
public void run() {
System.out.println("Runnable running");
}
}
// Method 3: Lambda Expression (Java 8+)
Thread t = new Thread(() -> System.out.println("Lambda thread"));class Counter {
private int count = 0;
public synchronized void increment() {
count++;
}
public int getCount() {
return count;
}
}# Clone the repository
git clone https://github.com/Shubh2-0/Multi-Threading.git
cd Multi-Threading
# Open in your IDE
# Run the examples and experiment!