Lecture 9: Concurrency Abstractions

Title slideIn this lecture I reviewed the built-in Java concurrency primitives, the challenges of writing thread-safe code, and introduced some patterns that can help: immutability, synchronization wrappers, and control abstractions like producer/consumer. More high-level currency abstractions, such as those in the java.util.concurrent package, support programming that is not only thread-safe but takes explicit advantage of concurrency to increase performance and responsiveness. Finally, I walked through some intentionally racy code in the Java String library.

Thanks to all of you who submitted the mid-course feedback survey: I summarised contributions in the lecture, and also reported on some things I’ve done in response.

Link: Slides for Lecture 9


1. Do this

Find out what the addAndGet method on a Java AtomicLong object does. Why is that useful?

Java 8 has a new LongAdder class. Find out what that does, and how it can make code faster.

2. Read this

Work through the remaining sections of the Java Concurrency Tutorial.


This is not required homework: it’s a coding exercise for those who want to try their hand at programming with Java concurrency.

Lecture 7 presented some code for one method of a Pigeonhole class. The exercise is to take that class and fill out other methods it needs: to check whether a pigeonhole is full or empty, and to release the contents of a pigeonhole. You might think of others.

Then write some code that exercises concurrent pigeonholes. For example, a PigeonFancier program that:

  • Has a fixed number of pigeonholes which are emptied by some dedicated pigeon-handler threads releasing pigeons after random delays;
  • Has a single thread which regularly puts new pigeons into empty holes;
  • Makes sure the pigeon stuffer doesn’t wait too long for any hole to become empty.

You might also be able to think of other ways that explore or stress the concurrency in this.


These two books are core references for Java concurrency, with clear explanations of the facilities offered by the language and libraries, as well as discussions of when and why to use them.

  • Brian Goetz, Time Peierls, Joshua Block, Joseph Bowbeer, David Holmes and Doug Lea. Java Concurrency In Practice. Addison-Wesley, 2006.

    The current standard reference for concurrent Java programming.

  • Doug Lea. Concurrent Programming in Java: Design Principles and Patterns. Second Edition. Addison-Wesley, 1999.

    The original text describing many of the patterns now implemented inside java.util.concurrent.

2016-10-23 Update. More references to things mentioned during the lecture: trains racing to Scotland; the latest Linux kernel vulnerability; data races and time travel in compilers; pseudo-random numbers and concurrency; racy String hashing.

Picture of a Great Northern Stirling 4-2-2 The Race to the North
Race condition arising at Kinnaber Junction south of Aberdeen.
Link: Wikipedia
Picture of a cow CVE-2016-5195
Race condition leading to serious vulnerability in the Linux kernel, publicised this week.
Link: ZDnet; Video; BBC; Logo
Screenshot Race Condition vs. Data Race
John Regehr
Race condition is a general term; data race is more specific; neither implies the other. This piece explains and gives examples.
Link: Blog article
Screenshot Fix your damned data races
Nicholas Nethercote, Mozilla
The dangers of programming with data races.
Link: Blog article
Screenshot Undefined behavior can result in time travel
Raymond Chen, Microsoft
C/C++ compiler surprises from Chen’s blog The Old New Thing.
Link: Blog article
Picture of Max Dyckhoff Deterministic Halo
Pseudo-randomness, threads, and network gameplay
Interview with Max Dyckhoff at Bungie
Links: Interview; Another, and picture source
Screenshot of Java code Racy String hashing
Intentional data race in the Java String class.
Link: Source for String.java.hashCode()