Java Lock-Free Concurrency: CAS Mechanism, Atomic APIs, and Unsafe Internals
Compare-And-Swap Principles and VolatileCompare-And-Swap (CAS) is an optimistic locking technique that reads a value V from memory, compares it with an expected value A, and if they match, writes a new value B to memory. If they do not match, the operation is retried until it succeeds.CAS is guaranteed to be atomic at the hardware level via the ...
Posted on Sun, 17 May 2026 06:38:53 +0000 by slipster70
Common Lock Strategies and Synchronized Implementation
Common Lock Strategies
Detailed Lock Strategies
1. Pessimistic Lock vs. Optimistic Lock
Criteria: When locking, predict whether the probability of lock contention is high or low.
If the probability is predicted to be high, more work is required, and locking overhead (time, system resources) is larger → Pessimistic Lock
If the probability is pr ...
Posted on Sat, 16 May 2026 00:15:43 +0000 by elenev
Understanding Compare-and-Swap and Atomic Variables in Java Concurrency
Atomicity is one of the three cornerstones of thread safety alongside visibility and ordering. A set of operations is atomic when it appears indivisible to other threads—either all steps complete or none do, with exclusive access at any moment. Visibility ensures that modifications made by one thread become immediately apparent to others; order ...
Posted on Thu, 14 May 2026 10:55:05 +0000 by rane500
How ConcurrentHashMap Achieves Thread Safety in JDK 8
Data Structure
The internal structure mirrors HashMap, consisting of a hash table array with linked lists for collision handling. When a bucket accumulates more than eight entries, it transforms into a red-black tree for optimized search performance.
Implementation Approach
JDK 8 leverages synchronized and CAS operations for concurrent access c ...
Posted on Fri, 08 May 2026 01:24:57 +0000 by FRSH