Home  Synchronization and concurrency  wait/notify  final  volatile  synchronized keyword  Java threading  Deadlock (and avoiding it)  Java 5: ConcurrentHashMap  Atomic variables  Explicit locks  Queues  Semaphores  CountDownLatch  CyclicBarrier

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.

Next...

On the next page, we move on to look at Java 5's concurrent collections, with arguably the most important, the ConcurrentHashMap class.

Article written by Neil Coffey (@BitterCoffey).

Software

 LetterMeister (word puzzle game for iPhone)
 Currency Quoter (currency converter/predictor)
 French Vocab Games for iPhone/iPad
 Vocabularium: create Spanish vocab podcasts


Java programming articles and tutorials on this site are written by Neil Coffey (@BitterCoffey). Suggestions are always welcome if you wish to suggest topics for Java tutorials or programming articles, or if you simply have a programming question that you would like to see answered on this site. Most topics will be considered. But in particular, the site aims to provide tutorials and information on topics that aren't well covered elsewhere, or on Java performance information that is poorly described or understood. Suggestions may be made via the Javamex blog (see the site's front page for details).
Copyright © Neil Coffey 2014. All rights reserved.