Java Reference
In-Depth Information
Listing 15.3. Preserving Multivariable Invariants Using CAS.
15.3.2. Performance Comparison: Locks Versus Atomic Variables
To demonstrate the differences in scalability between locks and atomic variables, we con-
structed a benchmark comparing several implementations of a pseudorandom number gener-
ator (PRNG). In a PRNG, the next “random” number is a deterministic function of the previ-
ous number, so a PRNG must remember the previous number as part of its state.
Listings 15.4 and 15.5 show two implementations of a thread-safe PRNG, one using
ReentrantLock and the other using AtomicInteger . The test driver invokes each re-
Search WWH ::




Custom Search