Card 09/ 09
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.
| The idea | Where |
|---|---|
A synchronized instance method locks this; the static form locks the class | static synchronized Locks the Class, Not the Instance |
| Releasing a lock publishes everything written inside it, to anyone taking the same lock | Lock Visibility: Releasing a Lock Publishes Every Write |
| Locks are reentrant: held by the thread, not the block | Reentrant Locks: Taking a Lock the Thread Already Holds |
ReentrantLock adds timeouts and interruptible waiting, and needs a finally | synchronized vs ReentrantLock |
| Every method being atomic says nothing about a sequence of two calls | Synchronised 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.
← → move through sections and cards