Is ThreadLocal Obsolete? A Detailed Comparison with ScopedValue
summary
Compares Java's ThreadLocal and the Java 21 ScopedValue as context-passing mechanisms, explaining why ScopedValue was introduced and whether ThreadLocal will be deprecated. Covers API/design differences and their impact on performance and memory, behavior with virtual threads and structured concurrency, and common anti-patterns that can break applications.
AI-written summary · about
chapters (16)
- 0:00Introduction
- 1:26Speakers
- 2:50ThreadLocal vs ScopedValue
- 8:07ThreadLocal internals
- 13:00ThreadLocal in child threads
- 14:46ScopedValue internals
- 23:58ScopedValue optimizations
- 25:26Memory usage
- 27:55Benchmark setup
- 31:03ScopedValue cache
- 33:08JVM performance factors
- 35:49Lambda memory impact
- 38:08Stack chunk overhead
- 38:51Performance benchmarks
- 41:44Key takeaways
- 43:07When to use each
