Card 09/ 09

RecapDifficulty: Advanced1 min

What a Lock Gives You, and What It Does Not

Five things this topic established, and where each one was.

What the topic established, and which card it was on
The thingWhere
A synchronized instance method locks this; the static form locks the classWhy a static synchronized Method Does Not Lock the Instance
Leaving a lock publishes everything written inside it, to anyone entering the same lockThe Second Guarantee
Locks are reentrant, held by the thread rather than the blockRe-entering a Lock You Are Already Holding
ReentrantLock adds timeouts and interruptibility, and needs a finallysynchronized Against ReentrantLock
Every method being atomic says nothing about a sequence of twoWhy Synchronizing Every Method Did Not Make the Class Thread-Safe

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 whole of the work, and the keyword does none of it for you.