Synchronization and concurrency
Deadlock (and avoiding it)
Java 5: ConcurrentHashMap
The atomic arrays: AtomicIntegerArray, AtomicLongArray and AtomicReferenceArray
As well as the atomic integer, long and reference classes described on the previous page, the Java 5 concurrency library provides atomic array implementations of these types. It has the usual set of methods for atomic access such as compareAndSet() and incrementAndGet(), with the first parameter of each being the index into the array.
You could achieve more or less similar functionality by creating, for example, an array of AtomicInteger objects. But these classes are likely to be more efficient on memory and nicer to the garbage collector, since the underlying array is a single object rather than several individual AtomicInteger (etc) objects. Under the hood, these classes actually plug into the JVM in order to atomically access a particular position in a volatile array. In other words, there's no general way exposed by the class libraries to immitate these atomic array classes as efficiently.
If you need an atomic boolean array or atomic bitset, there's none provided as standard. But of course you could use an AtomicIntegerArray and set elements to values of 0 or 1.
Atomic field updaters
A final part of the atomic variable jigsaw in Java 5 are the atomic field updaters. However, they are seldom used and you may wish to skip on to the next section.
On the next page, we move on to look at Java 5's concurrent collections, with arguably the most important, the ConcurrentHashMap class.
Copyright © Neil Coffey 2014. All rights reserved.