Recap

Working with shared mutable state is a huge burden that requires us to endure added complexity and the accompanied greater chances of error. When refactoring code, look out for some common concurrency-related mistakes:

  • Don’t create threads from within constructors; instead, create them in static factory methods—see Preserve Invariant.

  • Don’t create arbitrary threads; instead, use a pool of threads to reduce the tasks startup time and resource use—see Mind Your Resources.

  • Ensure that access to mutable fields cross memory barrier and are visible to threads properly—see Ensure Visibility.

  • Evaluate the granularity of locks and promote concurrency. Ensure that the locks are not overly conservative but are at the right level to provide adequate ...

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.