Chapter 8Favoring Isolated Mutability

There is always a better way.

Thomas Alva Edison

“If it hurts, stop doing it” is a doctor’s good advice. In concurrent programming, shared mutability is “it.”

With the JDK threading API, it’s easy to create threads, but it soon becomes a struggle to prevent them from colliding and messing up. The STM eases that pain quite a bit; however, in languages like Java, we must still be very careful to avoid unmanaged mutable variables and side effects. Surprisingly, the struggles disappear when shared mutability disappears.

Letting multiple threads converge and collide on data is an approach we’ve tried in vain. Fortunately, there’s a better way—event-based message passing. In this approach, we treat tasks as ...

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.