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

Loom is more than virtual threads: Structured C...

Loom is more than virtual threads: Structured Concurrency and Scoped Values

Most of the talk about Project Loom as been about JEP 425: Virtual Threads. That's great because virtual threads are incredibly useful and will revolutionize how we write high-throughput applications. But did you know there are two more JEPs associated with Project Loom that are also very useful? First we'll learn about JEP 453: Structured Concurrency and how it will simplify multi-threaded programming on the JVM. Next, we'll learn about JEP 487: Scoped Values, which aims to solve some problems that ThreadLocals have had since their introduction way back in Java 1.2! Come to this talk and we'll learn why these projects are useful additions to the JVM, and write some code to illustrate where you'll be able to use them effectively.

Avatar for Todd Ginsberg

Todd Ginsberg

August 14, 2025
Tweet

More Decks by Todd Ginsberg

Other Decks in Technology

Transcript

  1. Loom Is More Than Virtual Threads! KCDC 2025 2025-08-14 Todd

    Ginsberg Structured Concurrency and Scoped Values @ToddGinsberg Lead Engineer - Payments Deutsche Bank
  2. Hello! Todd Ginsberg Raleigh, NC TriJUG organizer 30 years of

    professional experience Currently: Director, Lead Engineer - Payments Deutsche Bank Cary, NC Photo Credit: Andrew Byala @todd.ginsberg.com
  3. @todd.ginsberg.com Project Loom is to intended to explore, incubate and

    deliver Java VM features and APIs built on top of them for the purpose of supporting easy-to-use, high-throughput lightweight concurrency and new programming models on the Java platform. https://wiki.openjdk.org/display/loom/Main
  4. @todd.ginsberg.com Project Loom is to intended to explore, incubate and

    deliver Java VM features and APIs built on top of them for the purpose of supporting easy-to-use, high-throughput lightweight concurrency and new programming models on the Java platform. https://wiki.openjdk.org/display/loom/Main
  5. @todd.ginsberg.com Project Loom is to intended to explore, incubate and

    deliver Java VM features and APIs built on top of them for the purpose of supporting easy-to-use, high-throughput lightweight concurrency and new programming models on the Java platform. https://wiki.openjdk.org/display/loom/Main
  6. @todd.ginsberg.com Project Loom: Timeline 9 10 18 19 20 21

    22 23 Loom Virtual Threads Released Preview Incubating 11 … 17 24 25 +
  7. @todd.ginsberg.com Project Loom: Timeline 9 10 18 19 20 21

    22 23 Loom Virtual Threads Structured Concurrency Released Preview Incubating 11 … 17 24 25 +
  8. @todd.ginsberg.com Project Loom: Timeline 9 10 18 19 20 21

    22 23 Loom Virtual Threads Structured Concurrency Scoped Values Released Preview Incubating 11 … 17 24 25 +
  9. @todd.ginsberg.com Project Loom: Timeline 9 10 18 19 20 21

    22 23 Loom Virtual Threads Structured Concurrency Scoped Values Released Preview Incubating 11 … 17 24 25 +
  10. @todd.ginsberg.com Platform Threads Operating System JVM OS Thread OS Thread

    OS Thread OS Thread OS Thread OS Thread OS Thread Platform Thread Platform Thread Platform Thread Platform Thread
  11. @todd.ginsberg.com Java Threads: The Downside Memory hungry Slow creation time

    Overhead for context switching 0% useful when blocked
  12. @todd.ginsberg.com Java Threads: The Downside Memory hungry Limited number by

    OS Slow creation time Overhead for context switching 0% useful when blocked
  13. @todd.ginsberg.com Platform Threads Operating System JVM OS Thread OS Thread

    OS Thread OS Thread OS Thread OS Thread OS Thread Platform Thread Platform Thread Platform Thread Platform Thread
  14. @todd.ginsberg.com Platform Threads Operating System JVM OS Thread OS Thread

    OS Thread OS Thread OS Thread OS Thread OS Thread Platform Thread Platform Thread Platform Thread Platform Thread
  15. @todd.ginsberg.com Virtual Threads Operating System JVM OS Thread OS Thread

    OS Thread OS Thread OS Thread OS Thread OS Thread Carrier Thread Carrier Thread Carrier Thread Carrier Thread
  16. @todd.ginsberg.com Virtual Threads Operating System JVM OS Thread OS Thread

    OS Thread OS Thread OS Thread OS Thread OS Thread Carrier Thread Carrier Thread Carrier Thread Carrier Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread
  17. @todd.ginsberg.com Virtual Threads Operating System JVM OS Thread OS Thread

    OS Thread OS Thread OS Thread OS Thread OS Thread Carrier Thread Carrier Thread Carrier Thread Carrier Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread
  18. @todd.ginsberg.com Virtual Threads Operating System JVM OS Thread OS Thread

    OS Thread OS Thread OS Thread OS Thread OS Thread Carrier Thread Carrier Thread Carrier Thread Carrier Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread
  19. @todd.ginsberg.com Virtual Threads Operating System JVM OS Thread OS Thread

    OS Thread OS Thread OS Thread OS Thread OS Thread Carrier Thread Carrier Thread Carrier Thread Carrier Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread
  20. @todd.ginsberg.com Virtual Threads Operating System JVM OS Thread OS Thread

    OS Thread OS Thread OS Thread OS Thread OS Thread Carrier Thread Carrier Thread Carrier Thread Carrier Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread
  21. @todd.ginsberg.com Virtual Threads Operating System JVM OS Thread OS Thread

    OS Thread OS Thread OS Thread OS Thread OS Thread Carrier Thread Carrier Thread Carrier Thread Carrier Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Platform Thread
  22. @todd.ginsberg.com Virtual Threads as Tasks Operating System JVM OS Thread

    OS Thread OS Thread OS Thread OS Thread OS Thread OS Thread Carrier Thread Carrier Thread Carrier Thread Carrier Thread Task Task Task Task Task Task Task Task Task Task Task Task Task Task Platform Thread
  23. @todd.ginsberg.com Virtual Threads as Tasks Operating System JVM OS Thread

    OS Thread OS Thread OS Thread OS Thread OS Thread OS Thread Carrier Thread Carrier Thread Carrier Thread Carrier Thread Task Task Task Task Task Task Task Platform Thread Task Task Task Task Task Task Task Task Task Task Task Task Task Task Task Task Task Task Task Task Task
  24. @todd.ginsberg.com What Virtual Threads Give Us Same API: java.lang.Thread Much

    lower memory requirements Number limited by heap memory
  25. @todd.ginsberg.com What Virtual Threads Give Us Same API: java.lang.Thread Much

    lower memory requirements Number limited by heap memory Much quicker creation time
  26. @todd.ginsberg.com What Virtual Threads Give Us Same API: java.lang.Thread Much

    lower memory requirements Number limited by heap memory Much quicker creation time Make better use of system resources
  27. @todd.ginsberg.com Project Loom is to intended to explore, incubate and

    deliver Java VM features and APIs built on top of them for the purpose of supporting easy-to-use, high-throughput lightweight concurrency and new programming models on the Java platform. https://wiki.openjdk.org/display/loom/Main
  28. @todd.ginsberg.com Project Loom is to intended to explore, incubate and

    deliver Java VM features and APIs built on top of them for the purpose of supporting easy-to-use, high-throughput lightweight concurrency and new programming models on the Java platform. https://wiki.openjdk.org/display/loom/Main
  29. @todd.ginsberg.com Final Thoughts on Virtual Threads Do Not Pool Virtual

    Threads Be open to new ways of writing concurrent code
  30. @todd.ginsberg.com Three Problems With Unstructured Concurrency Relationships between task and

    subtask don’t really exist Managing work is difficult, making the true purpose of code opaque Observability is difficult
  31. @todd.ginsberg.com Structured Concurrency: History • 2016 - Conceived by Martin

    Sústrik (golang) • 2017 - Nathaniel J. Smith “Nursery Pattern” in Python • Now shipping in Kotlin, Swift, and Java (Preview!)
  32. @todd.ginsberg.com Structured Concurrency: What Is It? Subtasks have well-defined entry

    and exit points Strict nesting of task lifetimes, like they are presented in code
  33. @todd.ginsberg.com Subtask Lifetime: Ideal Scenario public String doSomething() { String

    first = firstMethod(); String second = secondMethod(); return first + second; }
  34. @todd.ginsberg.com Subtask Lifetime: Ideal Scenario public String doSomething() { String

    first = firstMethod(); String second = secondMethod(); return first + second; }
  35. @todd.ginsberg.com Subtask Lifetime: Ideal Scenario public String doSomething() { String

    first = firstMethod(); String second = secondMethod(); return first + second; }
  36. @todd.ginsberg.com Subtask Lifetime: Ideal Scenario public String doSomething() { String

    first = firstMethod(); String second = secondMethod(); return first + second; }
  37. @todd.ginsberg.com Subtask Lifetime: Ideal Scenario public String doSomething() { String

    first = firstMethod(); String second = secondMethod(); return first + second; }
  38. @todd.ginsberg.com Subtask Lifetime: Ideal Scenario public String doSomething() { String

    first = firstMethod(); String second = secondMethod(); return first + second; }
  39. @todd.ginsberg.com Subtask Lifetime: Ideal Scenario public String doSomething() { String

    first = firstMethod(); String second = secondMethod(); return first + second; }
  40. @todd.ginsberg.com Java’s Structured Concurrency: Goals Promote a style of concurrent

    programming that can eliminate common risks arising from cancellation and shutdown, such as thread leaks and cancellation delays https://openjdk.org/jeps/505
  41. @todd.ginsberg.com Java’s Structured Concurrency: Goals Promote a style of concurrent

    programming that can eliminate common risks arising from cancellation and shutdown, such as thread leaks and cancellation delays https://openjdk.org/jeps/505
  42. @todd.ginsberg.com Java’s Structured Concurrency: Goals Promote a style of concurrent

    programming that can eliminate common risks arising from cancellation and shutdown, such as thread leaks and cancellation delays Improve the observability of concurrent code https://openjdk.org/jeps/505
  43. @todd.ginsberg.com Remember: Structured Concurrency Explicit relationship between tasks and subtasks

    Managing work is easier (or automatic) making code intent clearer Observability is easier
  44. @todd.ginsberg.com Quick Refresher: ThreadLocal ThreadLocal<String> accessLevel = ... void someOtherPlace()

    { switch(accessLevel.get()) { case "GUEST" -> guestLevelWork(); case "ADMIN" -> adminLevelWork(); } }
  45. @todd.ginsberg.com Quick Refresher: ThreadLocal ThreadLocal<String> accessLevel = ... void someOtherPlace()

    { switch(accessLevel.get()) { case "GUEST" -> guestLevelWork(); case "ADMIN" -> adminLevelWork(); } }
  46. @todd.ginsberg.com Problem 1: Unconstrained Mutability void quickHack() { var original

    = accessLevel.get(); accessLevel.set("ADMIN"); doTheThing(); accessLevel.set(original); }
  47. @todd.ginsberg.com Problem 1: Unconstrained Mutability void quickHack() { var original

    = accessLevel.get(); accessLevel.set("ADMIN"); doTheThing(); accessLevel.set(original); }
  48. @todd.ginsberg.com Problem 1: Unconstrained Mutability void quickHack() { var original

    = accessLevel.get(); accessLevel.set("ADMIN"); doTheThing(); accessLevel.set(original); }
  49. @todd.ginsberg.com How Do Scoped Values Help? Unconstrained Mutability Values are

    Immutable Unbounded Lifetime Defined Lifetime Expensive Inheritance Cheap/Free Inheritance
  50. @todd.ginsberg.com Scoped Values: Remember Useful anywhere you need to pass

    data one way Try to migrate to Scoped Values when you have structured sharing use cases
  51. @todd.ginsberg.com Nothing Is Going Away “... remove the traditional implementation

    of threads, or to silently migrate existing applications to use virtual threads.” It is not a goal to… https://openjdk.org/jeps/444
  52. @todd.ginsberg.com Nothing Is Going Away “... replace any of the

    concurrency constructs in the java.util.concurrent package, such as ExecutorService and Future.” It is not a goal to… https://openjdk.org/jeps/505
  53. @todd.ginsberg.com Nothing Is Going Away “... require migration away from

    thread-local variables, or to deprecate the existing ThreadLocal API.” It is not a goal to… https://openjdk.org/jeps/506