Upgrade to Pro — share decks privately, control downloads, hide ads and more …

More Action, More Overview - Java 25 Edition

More Action, More Overview - Java 25 Edition

Java has undergone many iterations and enhancements in its 25 versions. Among other things, work is currently underway to improve concurrent programming.

Virtual threads enable millions of parallel execution threads, making the simple thread-per-request programming model usable. However, millions of threads also bring new challenges for developers and the JVM. Structured concurrency brings order to the chaos, and scoped values enable elegant data sharing.

In this session, we will focus on Java's new concurrency model. The emphasis will be on scoped values and their impact on us as developers.

Avatar for Merlin Bögershausen

Merlin Bögershausen

September 15, 2025
Tweet

More Decks by Merlin Bögershausen

Other Decks in Programming

Transcript

  1. ©2025 Moderne, Inc. Who sees a problem? Quick POLL POLL

    2 for (int i = 0; i < 1_000_000; i++) { } new Thread(() -> processOrder()).start();
  2. ©2024 Moderne, Inc. 18 The Money Slide 1 MB per

    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
  3. ©2024 Moderne, Inc. 5 Rule Engine Example class Engine {

    interface Rule { void fire(); } void execute(List<Rule> rules, Map<String, String> data) {} } Rule Engine with 100.000 Rules executed in parallel GitHub GIST
  4. ©2024 Moderne, Inc. 7 Java Thread - Basics // Platform

    Thread = OS Thread = 1MB new Thread(() -> doWork()).start(); // Workaround: Thread Pools, ... Executors.newFixedThreadPool(100);
  5. ©2024 Moderne, Inc. 8 Virtual Threads – Java 21 for

    (int i = 0; i < 1_000_000; i++) { // 1 Virtual Thread = 1KB Thread.startVirtualThread(() -> doWork()); } // Retrofit to Executors try (var executor = Executors.newVirtualThreadPerTaskExecutor()) { IntStream.range(0, 1_000_000) .forEach(i -> executor.submit(() -> doWork())); }
  6. ©2024 Moderne, Inc. 10 Virtual Threads Architecture Virtual Thread Carrier

    Thread OS Thread 1 KB Heap ForkJoinPool 1 MB Stack
  7. ©2024 Moderne, Inc. 11 • I/O-bound operations DB, REST calls

    • 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!) Avoide for
  8. ©2024 Moderne, Inc. 14 Scoped Values API // JEP 506

    - Final in Java 25! private static final ScopedValue<SSLContext> SSL_CTX = ScopedValue.newInstance(); private static final ScopedValue<Map<String, String>> CONTEXT = ScopedValue.newInstance(); // Setzen und weitergeben ScopedValue.where(SSL_CTX, sslContext) .where(CONTEXT, data) .run(() -> processWithContext());
  9. ©2024 Moderne, Inc. 15 • Mutable data • Not memory

    Save • Copy per (virtual) Thread • Slower due to Implementation ThreadLocal What about ThreadLocal • Immutable Data • Memory Save • Designed for Virtual Threads • Efficient Implementation ScopedValue
  10. ©2024 Moderne, Inc. 18 StructuredTaskScope API try (var scope =

    StructuredTaskScope.open()) { Subtask<String> task = scope.fork(() -> doWork()); scope.join(); // void return return task.get(); // wirft Exception bei Fehler }
  11. ©2024 Moderne, Inc. 19 Concurrent Rule void fire() { try

    (var scope = StructuredTaskScope.open()) { Subtask<String> name = scope.fork(() -> readName()); Subtask<Address> address = scope.fork(() -> readAddress()); scope.join(); System.out.printf("%s's home is %s %d, %s%n", name.get(), address.get().street(), address.get().house(), address.get().city()); } catch (InterruptedException e) { throw new RuntimeException(e); } }
  12. ©2024 Moderne, Inc. 22 How to Migrate 1. Virtual Threads

    (Final seit Java 21) → Sofort migrieren für I/O-bound workloads 2. Scoped Values (Final in Java 25) → ThreadLocal ersetzen wo immutable 3. Structured Concurrency (Fifth Preview) → Experimentieren, aber nicht in Production 4. Stable Values (Preview in Java 25) → Beobachten für lazy initialization
  13. ©2024 Moderne, Inc. 23 Best Practices Thread.startVirtualThread() für neue Projekte

    Executors.newVirtualThreadPerTaskExecutor() ScopedValues für immutable context Millionen von Virtual Threads sind OK! NIEMALS Virtual Threads poolen Keine ThreadLocals mit Virtual Threads Nicht für CPU-intensive Tasks Kein synchronized mit Virtual Threads (Pinning!)
  14. ©2024 Moderne, Inc. 25 Resources Code & Examples: github.com/MBoegers/87fa0928c46e037155dbc7d48aaf7553 Compile

    & Run: javac --release 25 --enable-preview *.java java --enable-preview Main JEPs: • JEP 444: Virtual Threads (Final) • JEP 506: Scoped Values (Final) • JEP 505: Structured Concurrency (Preview) • JEP 502: Stable Values (Preview) GitHub GIST