Using Java 8 Lambdas and StampedLock To Manage Thread Safety
summary
Claim: StampedLock optimistic reads let readers proceed without blocking writers, upgrade reads to writes (tryConvert), and often outperform ReentrantReadWriteLock for multi-field invariants; drawback: not reentrant and validate can force fallback to a pessimistic read. Demo: converting a primitive Trove map to use StampedLock yields much higher throughput. Alternatives shown: nonblocking AtomicReference/compare-and-swap, Unsafe, and VarHandles as safe Unsafe alternative (Java 9 compareAndExchange returns prior value but can be slower under high contention). Lambdas make lock idioms pluggable but checked exceptions complicate use.
AI-written summary · about
chapters (19)
- 0:00Introduction to talk
- 1:06What is StampedLock
- 3:57StampedLock advantages
- 8:59Crocodile analogy for invariants
- 10:01Pessimistic read with StampedLock
- 11:24Optimistic read validation
- 15:36Value holder objects approach
- 19:57AtomicReferenceFieldUpdater
- 23:50Why avoid Unsafe
- 25:19VarHandles introduction
- 27:05Compare-and-exchange in Java 9
- 31:03Conditional read-to-write upgrade
- 32:58StampedLock vs synchronized
- 37:17AutoCloseable locks and lambdas
- 41:10Lambda checked exceptions issue
- 44:21Lambda idioms for concurrency
- 46:50Future of concurrency patterns
- 48:34Synchronized vs ReentrantLock
- 50:00When to use StampedLock
