Card 09/ 09
All 9 cards
RecapDifficulty: Advanced1 min
What a Lock Gives You, and What It Does Not
Five things this topic established, and where each one was.
| The thing | Where |
|---|---|
A synchronized instance method locks this; the static form locks the class | Why a static synchronized Method Does Not Lock the Instance |
| Leaving a lock publishes everything written inside it, to anyone entering the same lock | The Second Guarantee |
| Locks are reentrant, held by the thread rather than the block | Re-entering a Lock You Are Already Holding |
ReentrantLock adds timeouts and interruptibility, and needs a finally | synchronized Against ReentrantLock |
| Every method being atomic says nothing about a sequence of two | Why 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.