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 ...