Monitors and synchronization
In this section, we will study inter-thread communication using the synchronized
statement in more detail. As we saw in the previous sections, the synchronized
statement serves both to ensure the visibility of writes performed by different threads, and to limit concurrent access to a shared region of memory. Generally speaking, a synchronization mechanism that enforces access limits on a shared resource is called a lock. Locks are also used to ensure that no two threads execute the same code simultaneously; that is, they implement mutual exclusion.
As mentioned previously, each object on the JVM has a special built-in monitor lock, also called the intrinsic lock. When a thread calls the synchronized
statement on an ...
Get Learning Concurrent Programming in Scala - Second Edition 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.