Skip to content

Compound actions on shared state, such as incrementing a…

“Compound actions on shared state, such as incrementing a hit counter (read-modify-write) or lazy initialization (check-then-act), must be made atomic to avoid race conditions. Holding a lock for the entire duration of a compound action can make that compound action atomic. However, just wrapping…” quote by Brian Goetz
Download Open image
““Compound actions on shared state, such as incrementing a hit counter (read-modify-write) or lazy initialization (check-then-act), must be made atomic to avoid race conditions. Holding a lock for the entire duration of a compound action can make that compound action atomic. However, just wrapping the compound action with a synchronized block is not sufficient; if synchronization is used to coordinate access to a variable, it is needed everywhere that variable is accessed. Further, when using locks to coordinate access to a variable, the same lock must be used wherever that variable is accessed.””

Brian Goetz

About This Quote

This interpretation was drafted with AI assistance. It is one reading of the quote, not the author's own explanation.

Ensuring thread safety requires making compound operations on shared data atomic, and using the same lock consistently wherever the data is accessed.

In simple terms: Make shared actions atomic and lock consistently.

Key Takeaway

Always lock the same variable everywhere.

Themes

concurrency thread safety locking

Mood

cautious analytical

Type

technical educational

When to use this quote

  • multithreaded programming
  • database transactions
  • shared caches
  • parallel processing

Key Concepts

atomicity race conditions shared state

Questions to Reflect On

  • How do you decide the granularity of locking?
  • What alternatives exist to coarse-grained locks?
A Different Perspective

Locks can cause contention and reduce performance if overused.

★ ★ ★ ★ ★ No ratings yet

More by Brian Goetz

Explore all 13 Brian Goetz quotes

More Concurrency quotes

Browse all 13 Concurrency quotes