🛑️ Stop Using ThreadLocal! Migrate to Java Scoped Values
Автор: Carlos Chacin
Загружено: 2025-10-18
Просмотров: 102
ThreadLocal worked great for traditional threads — but with Java 21's Virtual Threads, it causes memory bloat and mutable state chaos. Java's Scoped Values (JEP 506) fix this: immutable, memory-efficient, and designed for modern concurrency. This 13-minute migration guide shows you how to replace ThreadLocal with Scoped Values step-by-step.
🎯 What You'll Learn:
• Why ThreadLocal breaks down with Virtual Threads (memory footprint explosion)
• How Scoped Values provide immutable, bounded-lifetime context sharing
• Step-by-step migration: ThreadLocal → Scoped Values API
• Code examples: Before/After comparison with real use cases
• When to use Scoped Values vs when ThreadLocal is still valid
• Integration with Structured Concurrency and Project Loom
• Testing strategies for migrated code
⏱️ Timestamps:
00:00 - Intro
00:20 - ThreadLocal Problems
00:40 - Trace Filter with ThreadLocal
03:20 - ThreadLocal Problems
04:40 - Testing it
07:20 - Migration to ScopeValue
12:30 - Outro
💡 Key Takeaway: Scoped Values eliminate ThreadLocal's memory leaks and mutability issues in Virtual Thread environments. Perfect for web request context, user sessions, database transactions — anything you'd share across thread boundaries!
✨ Java Features Used:
• ScopedValue API (JEP 506 - finalized in Java 25)
• Virtual Threads (JEP 444 - Java 21+)
• Structured Concurrency integration (JEP 462)
• Immutable context propagation patterns
🔗 Related Videos:
• Stop Using Try-Catch: Java 25 Pattern Matching: • 🛑️ Stop Using Try-Catch: Java 25 Pattern M...
• Java Records vs Kotlin Data Classes: • Java Records vs. Kotlin Data Classes: SECR...
📚 Official Resources:
• JEP 506 - Scoped Values: https://openjdk.org/jeps/506
• Oracle Scoped Values Guide: https://docs.oracle.com/en/java/javas...
• Project Loom Documentation: https://openjdk.org/projects/loom/
#java #java21 #scopedvalues #threadlocal #virtualthreads #projectloom #jep506 #concurrency
🔔 Subscribe for Java 21/25 deep-dives → Next: Scoped Values + Structured Concurrency!
📺️ VIDEOS TO WATCH NEXT:
Frameworkless REST APIs with Servlets, JDBC & PostgreSQL: • Frameworkless REST APIs with Servlets, JDB...
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: