"Everybody who learns concurrency and thinks they understand it, ends up finding mysterious races they thought weren't possible, and discovers that they didn't actually understand it yet after all."
While investigating the fundamental primitives of concurrency in Chapter 2, Concurrency on the JVM and the Java Memory Model, we recognized the need for protecting parts of the program from shared access. We saw that a basic way of achieving this isolation is the
synchronized statement, which uses intrinsic object locks to ensure that at most a single thread executes a specific part of the program at the same time. The disadvantage of using locks is that they can easily cause deadlocks, a situation in which the program cannot progress.
In this chapter, we will introduce Software Transactional Memory (STM), a concurrency control mechanism for controlling access to shared memory, which greatly reduces the risk of deadlocks and races. An STM...