Card 09/ 09

RecapDifficulty: Advanced1 min

Recap: Locks

Which object is locked, what a lock publishes, reentrancy, ReentrantLock, and sequences of calls.

Locks come down to the five ideas below, each listed with the card that covers it.

What the topic established, and which card it was on
The ideaWhere
A synchronized instance method locks this; the static form locks the classstatic synchronized Locks the Class, Not the Instance
Releasing a lock publishes everything written inside it, to anyone taking the same lockLock Visibility: Releasing a Lock Publishes Every Write
Locks are reentrant: held by the thread, not the blockReentrant Locks: Taking a Lock the Thread Already Holds
ReentrantLock adds timeouts and interruptible waiting, and needs a finallysynchronized vs ReentrantLock
Every method being atomic says nothing about a sequence of two callsSynchronised Methods Do Not Make a Sequence of Calls Atomic

A lock protects a region, and the region has to be as large as the operation. Deciding where the operation really starts and ends is the real work, and the keyword does none of it for you.