Dev Tools · 2h ago
Java 21's Scoped Values Offer Safer Alternative to ThreadLocal
ThreadLocal, a Java feature since 1.2, stores per-thread data but causes memory leaks and contamination in thread pools. Java 21 introduces Scoped Values as a modern, immutable alternative designed for Virtual Threads. Developers should migrate to Scoped Values for new code to avoid these pitfalls.
Meridian48 take
Scoped Values fix ThreadLocal's core flaws but require a mindset shift; adoption will be gradual as legacy codebases stick with the old pattern.
Read the full reporting
🧵 ThreadLocal no Java 21 – o que mudou, o que continua e quando você deve (ou não) usar →
DEV Community
java-21scoped-values