Selecting a Design Approach

We can avoid most of the issues with concurrency by opting for isolated mutability or pure immutability. For the most part, it’s easier to program with isolated mutability than with pure immutability.

With isolated mutability, we must ensure that the mutable variable is in fact isolated and never escapes to more than one thread. We also need to ensure that the messages we pass between threads are immutable. Use either the concurrent API provided with the JDK or one of the actor-based concurrency frameworks for message passing.

Designing for pure immutability requires more effort. It’s harder to realize it in applications that use object-oriented decomposition more than a functional decomposition. We have to devise ...

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.