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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access