Collections and Transactions

As we work through the examples, it’s easy to forget that the values we’re dealing with are, and should be, immutable. Only the identity is mutable, not the state value. Although STM makes life easy, it can be a challenge to ensure good performance while maintaining immutability.

Take the first step to ensure immutability. Make value classes final, and mark all their fields final (vals in Scala). Then transitively ensure that the classes for each field the values use are immutable as well. It should be first nature to make fields and classes final—this is the first step toward avoiding concurrency issues.

Although immutability will make code better and safer, one prominent reason that programmers are reluctant ...

Get Programming Concurrency on the JVM now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.