Java की Atomic Classes
In this page:
AtomicInteger counter = new AtomicInteger(initialValue);
counter.incrementAndGet();
counter.compareAndSet(expected, newValue);
Why Atomic Classes?
Atomic classes, जो java.util.concurrent.atomic में define हैं, किसी single variable पर lock-free, thread-safe operations प्रदान करती हैं। वे values को सुरक्षित रूप से update करने के लिए CPU-level Compare-And-Swap (CAS) instructions पर निर्भर करती हैं, साधारण updates के लिए एक traditional lock acquire और release करने का overhead टालते हुए।
उदाहरण: Why Atomic Classes?
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
public static void main(String[] args) {
AtomicInteger counter = new AtomicInteger(0); // lock-free, CAS-based updates
counter.incrementAndGet();
System.out.println(counter.get());
}
}
Login to try C/C++/Java/PHP code in the editor
AtomicInteger Basics
AtomicInteger built-in रूप से thread-safe increment और decrement operations प्रदान करता है -- incrementAndGet() जैसा method ++i जैसा ही काम करता है, लेकिन इसे atomically करता है, इसलिए एक ही counter को increment करने वाले concurrent threads कभी race condition में update नहीं खोते।
उदाहरण: AtomicInteger Basics
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
public static void main(String[] args) throws InterruptedException {
AtomicInteger counter = new AtomicInteger(0);
Runnable task = () -> { for (int i = 0; i < 1000; i++) counter.incrementAndGet(); };
Thread t1 = new Thread(task), t2 = new Thread(task);
t1.start(); t2.start();
t1.join(); t2.join();
System.out.println(counter.get()); // reliably 2000
}
}
Login to try C/C++/Java/PHP code in the editor
AtomicBoolean Toggles
compareAndSet(expected, update) AtomicBoolean के पीछे का core operation है: यह सिर्फ तभी नई value लिखता है जब variable की current value अब भी आपकी उम्मीद से मेल खाती हो, जो वह तरीका है जिससे CAS-based classes कभी block किए बिना conflicting concurrent updates को detect और reject करती हैं।
उदाहरण: AtomicBoolean Toggles
import java.util.concurrent.atomic.AtomicBoolean;
public class Main {
public static void main(String[] args) {
AtomicBoolean flag = new AtomicBoolean(false);
boolean changed = flag.compareAndSet(false, true); // only writes if current value matches expected
System.out.println(changed + " " + flag.get());
}
}
Login to try C/C++/Java/PHP code in the editor
AtomicReference for Objects
AtomicReference आपको threads के बीच किसी object reference को atomically और सुरक्षित रूप से update करने देता है, जो shared configuration objects, immutable state snapshots, या किसी भी mutable-reference field को manage करने के लिए उपयोगी है जिसे अन्यथा आपको एक lock से सुरक्षित करना होता।
उदाहरण: AtomicReference for Objects
import java.util.concurrent.atomic.AtomicReference;
public class Main {
public static void main(String[] args) {
AtomicReference<String> config = new AtomicReference<>("v1");
config.set("v2"); // atomic, safe across threads
System.out.println(config.get());
}
}
Login to try C/C++/Java/PHP code in the editor
Performance of Atomic Operations
Atomic operations कभी किसी thread को उस तरह block नहीं करते जैसे एक traditional lock कर सकता है, इसलिए low-to-moderate contention के तहत वे synchronized locking से तेज़ चलते हैं और कम CPU resources उपयोग करते हैं। बहुत ज़्यादा contention के तहत, हालांकि, बार-बार CAS retries अब भी performance घटा सकते हैं, इसलिए ये locks का universal विकल्प नहीं हैं।
उदाहरण: Performance of Atomic Operations
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
public static void main(String[] args) {
AtomicInteger counter = new AtomicInteger(0);
long start = System.nanoTime();
for (int i = 0; i < 100000; i++) counter.incrementAndGet(); // no blocking under low contention
System.out.println("Took " + (System.nanoTime() - start) + "ns");
}
}
Login to try C/C++/Java/PHP code in the editor
counter.set(counter.get() + 1)लिखना, जो दो अलग steps हैं जिन्हें कोई दूसरा thread बीच में रोक सकता है;incrementAndGet()का उपयोग करें।- यह मान लेना कि दो atomic variables को एक के बाद एक update करना जोड़ी के रूप में atomic है, जबकि कोई दूसरा thread बीच की state देख सकता है।
- यह उम्मीद करना कि
AtomicReference.compareAndSetequalsउपयोग करेगा, जबकि यह==से references compare करता है।
Chapter Quiz — Complete all 19 topics to unlock
0/19 topics done
Complete these topics first:
- Java Reflection API
- Java Annotations Advanced
- Java Garbage Collection
- Java Memory Management
- Java Performance Optimization
- Java Advanced Interview Questions
- Java CompletableFuture
- Java Atomic Classes
- Java Locks & Semaphores
- Java Concurrent Collections
- Java Cryptography Basics
- Java Hashing (MD5, SHA)
- Java SSL & HTTPS
- Java Logging (Log4j/SLF4J)
- Java Serialization Advanced
- Java Interview Questions Advanced
- Java Connection Pooling
- Java Test Driven Development
- Java Integration Testing