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

Todd Ginsberg April 14, 2024 0 3 Loom is More Than Virtual Threads: Structured Concurrency and Scoped Values

Todd Ginsberg April 14, 2024 0 3 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 428: Structured Concurrency and how it will simplify multithreaded programming on the JVM. Next, we'll learn about JEP 429: 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.

Todd Ginsberg

April 15, 2024
Tweet

More Decks by Todd Ginsberg

Other Decks in Technology

Transcript

  1. Loom Is More Than Virtual Threads! TriJUG 2024-04-15 Todd Ginsberg

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

    professional experience Currently: Director, Lead Engineer - Payments Deutsche Bank Cary, NC Photo Credit: Andrew Byala
  3. @ToddGinsberg 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. @ToddGinsberg 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. @ToddGinsberg 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. @ToddGinsberg Project Loom: Timeline 9 10 11 12 13 14

    15 16 17 18 19 20 21 22 23 Loom Virtual Threads Released Preview Incubating
  7. @ToddGinsberg Project Loom: Timeline 9 10 11 12 13 14

    15 16 17 18 19 20 21 22 23 Loom Virtual Threads Structured Concurrency Released Preview Incubating
  8. @ToddGinsberg Project Loom: Timeline 9 10 11 12 13 14

    15 16 17 18 19 20 21 22 23 Loom Virtual Threads Structured Concurrency Scoped Values Released Preview Incubating
  9. @ToddGinsberg Project Loom: Timeline 9 10 11 12 13 14

    15 16 17 18 19 20 21 22 23 Loom Virtual Threads Structured Concurrency Scoped Values Released Preview Incubating ? ?
  10. @ToddGinsberg 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. @ToddGinsberg Java Threads: The Downside Memory hungry Slow creation time

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

    OS Slow creation time Overhead for context switching 0% useful when blocked
  13. @ToddGinsberg 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. @ToddGinsberg 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. @ToddGinsberg 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. @ToddGinsberg 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. @ToddGinsberg 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. @ToddGinsberg 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. @ToddGinsberg 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. @ToddGinsberg Carrier Thread Usage 1 2 3 1 1 2

    Carrier Virtual 1 Virtual 2 Virtual 3 Runnable Running Waiting 3
  21. @ToddGinsberg 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
  22. @ToddGinsberg 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
  23. @ToddGinsberg 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
  24. @ToddGinsberg 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
  25. @ToddGinsberg What Virtual Threads Give Us Same API: java.lang.Thread Much

    lower memory requirements Number limited by heap memory
  26. @ToddGinsberg What Virtual Threads Give Us Same API: java.lang.Thread Much

    lower memory requirements Number limited by heap memory Much quicker creation time
  27. @ToddGinsberg 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
  28. @ToddGinsberg 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. @ToddGinsberg 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
  30. @ToddGinsberg Final Thoughts on Virtual Threads Do Not Pool Virtual

    Threads Be open to new ways of writing concurrent code
  31. @ToddGinsberg 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
  32. @ToddGinsberg 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!)
  33. @ToddGinsberg Structured Concurrency: What Is It? Subtasks have well-defined entry

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

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

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

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

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

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

    first = firstMethod(); String second = secondMethod(); return first + second; }
  40. @ToddGinsberg Subtask Lifetime: Ideal Scenario public String doSomething() { String

    first = firstMethod(); String second = secondMethod(); return first + second; }
  41. @ToddGinsberg 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/462
  42. @ToddGinsberg 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/462
  43. @ToddGinsberg 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/462
  44. @ToddGinsberg Remember: Structured Concurrency Explicit relationship between tasks and subtasks

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

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

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

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

    = accessLevel.get(); accessLevel.set("ADMIN"); doTheThing(); accessLevel.set(original); }
  49. @ToddGinsberg Problem 1: Unconstrained Mutability void quickHack() { var original

    = accessLevel.get(); accessLevel.set("ADMIN"); doTheThing(); accessLevel.set(original); }
  50. @ToddGinsberg How Do Scoped Values Help? Unconstrained Mutability Values are

    Immutable Unbounded Lifetime Defined Lifetime Expensive Inheritance Cheap/Free Inheritance
  51. @ToddGinsberg 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
  52. @ToddGinsberg 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
  53. @ToddGinsberg 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/462
  54. @ToddGinsberg 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/464