More action, more overview - From 4k to 1M Threads
Java 21+ Virtual Threads replace heavyweight platform threads with lightweight alternatives. But the real problem for teams is the migration path and how to utilize AI here.
Thread ~ 10.000 Threads max 1 ms creation time 1 KB per Thread 1.000.000+ Threads possible Created in 1 μs 1000 x 100 x 1000 x Platform Thread Virtual Thread Impact https://docs.oracle.com/en/java/javase/21/core/virtual-threads.html https://www.infoq.com/articles/java-virtual-threads-a-case-study/ https://github.com/ebarlas/java-httpserver-vthreads
• High concurrency >1000 concurrent tasks • Request-per-thread model Great us if When to Use Virtual Threads • CPU-intensive calculations • Thread pools (NEVER pool Virtual Threads!) Avoid for
Save • Copy per (virtual) Thread • Slower due to Implementation ThreadLocal What about ThreadLocal • Immutable Data • Memory Save • Designed for Virtual Threads • Efficient Implementation ScopedValue
(Final seit Java 21) → migrate if I/O-bound workloads 2. Scoped Values (Final in Java 25) → ThreadLocal replace where immutable, use recipe 3. Structured Concurrency (Sixth Preview) → experiment and feedback; not in prod, yet 4. Lazy Constants (Preview in Java 25 & 26) → Check for lazy initialization Pro tip: Use tools to Understand surface
immutable context A million Virtual Threads is fine! NEVER pool Virtual Threads No ThreadLocals with Virtual Threads Not for CPU-intensive Tasks Pro tip: Use tools to Understand surface === Combined total === Uses: 105 Input: 177 Output: 21,306 Cache create: 276,238 Cache read: 2,135,980 Total tokens: 2,433,701 === Combined total === Uses: 12 Input: 22 Output: 1,794 Cache create: 11,291 Cache read: 203,578 Total tokens: 216,685 Propper Tools