Java FAQ: Top Questions
51. What is the Atomic classes in Java?
Atomic classes
in
java.util.concurrent.atomic
, provide thread-safe operations on single variables without locks, using hardware-level atomic instructions like compare-and-swap (CAS).
Key points:
-
Classes:
AtomicInteger
,AtomicLong
,AtomicReference
,AtomicBoolean
, etc. -
Operations:
get
,set
,compareAndSet
,getAndIncrement
. - Advantages: Lock-free, high performance for simple updates.
- Use Case: Counters, flags, or shared references in concurrent applications.
- Limitations: Not suitable for complex, multi-step operations.