Introduction: Thread Concurrency Kya Hota Hai?
Thread concurrency ka matlab hota hai:
“Jab ek hi program mein multiple threads simultaneously ek resource ko access ya modify karte hain.”
Yeh powerful feature hai lekin agar sahi se handle na kiya jaye to data inconsistency ho sakti hai.
Real-World Example:
Socho ek ATM machine mein 2 log ek hi account se simultaneously withdrawal karte hain.
- Balance hai ₹1000
- Dono thread check karte hain: balance = ₹1000
- Dono ₹800 nikaal lete hain
- Final balance hona chahiye ₹200, lekin ho gaya -₹600
Yahi hota hai concurrency problem.
Java mein Concurrency Problems Kab Hote Hain?
- Jab threads ek shared resource pe simultaneously access karte hain.
- Examples: variables, files, database, objects, etc.
Solution: Synchronization
1. Using synchronized
keyword
class SharedCounter {
int count = 0;
public synchronized void increment() {
count++;
}
}
synchronized
ensure karta hai ki ek samay mein ek hi thread critical section access kare.- Prevents race condition.
Without Synchronization Example
class Counter {
int count = 0;
public void increment() {
count++;
}
}
public class NoSyncDemo {
public static void main(String[] args) throws InterruptedException {
Counter c = new Counter();
Thread t1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) c.increment();
});
Thread t2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) c.increment();
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("Final Count: " + c.count); // Wrong output possible
}
}
Output:
Final count kabhi 2000 nahi aata — kyunki count++
safe nahi hai.
With Synchronization
class Counter {
int count = 0;
public synchronized void increment() {
count++;
}
}
public class SyncDemo {
public static void main(String[] args) throws InterruptedException {
Counter c = new Counter();
Thread t1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) c.increment();
});
Thread t2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) c.increment();
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("Final Count: " + c.count); // Output: 2000 ✅
}
}
Ye synchronized
method race condition se bachata hai.
Other Concurrency Tools in Java
synchronized
: Basic lockingReentrantLock
: Advanced lockingAtomicInteger
: Lock-free thread-safe countersvolatile
: Ensures visibility of variable across threadsExecutorService
: Thread pool manage karne ke liye
AtomicInteger Example
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicExample {
static AtomicInteger count = new AtomicInteger(0);
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
for(int i=0; i<1000; i++) count.incrementAndGet();
});
Thread t2 = new Thread(() -> {
for(int i=0; i<1000; i++) count.incrementAndGet();
});
t1.start(); t2.start();
t1.join(); t2.join();
System.out.println("Final Count: " + count); // Safe and fast
}
}
Best Practices for Thread Concurrency
- Shared data access? → Synchronize it
- Performance chahiye? → Use Atomic classes
- Complex threads? → Use Executors and ThreadPools
- Avoid deadlocks (jab do threads ek dusre ka wait karte hain forever)
Conclusion
Thread concurrency Java mein powerful hai, lekin uske sath responsibly kaam karna zaroori hai.
Synchronization ya Atomic classes ka use karke aap safe and efficient concurrent apps bana sakte ho.
Comments