Slide 1

Slide 1 text

Balkrishna Rawool Virtual Threads, Structured Concurrency and Scoped Values: Putting it all together 22 May 2025

Slide 2

Slide 2 text

2

Slide 3

Slide 3 text

3 Balkrishna Rawool IT Chapter Lead, ING Bank @BalaRawool

Slide 4

Slide 4 text

4 Project Loom Purpose: Ø Supporting easy-to-use, high-throughput lightweight concurrency and new programming models for it Features: Ø Virtual Threads – Final in JDK 21 Ø Structured Concurrency – Preview Ø Scoped Values – Final in JDK 25

Slide 5

Slide 5 text

Virtual Threads

Slide 6

Slide 6 text

6 Platform Threads and Virtual Threads JVM OS Platform Threads Platform Threads: Ø Thin wrapper over OS threads Ø One-to-one mapping with OS threads Ø Thread pools Virtual Threads Virtual Threads: Ø Lightweight user threads Ø Highly scalable Ø No need for pooling

Slide 7

Slide 7 text

7 Virtual Thread Scheduler Platform/ Carrier Thread Virtual Threads

Slide 8

Slide 8 text

A simple web-app with virtual threads

Slide 9

Slide 9 text

9 A simple web-app with virtual threads Ø SpringBoot App Ø SpringBoot 3.4 Ø JDK 25 (Project Loom early access build) Ø Domain: Banking Ø Name: A Bank

Slide 10

Slide 10 text

Structured Concurrency

Slide 11

Slide 11 text

11 Virtual threads enable Structured Concurrency

Slide 12

Slide 12 text

12 Programs à Structured Programming Concurrent programs à Structured Concurrency

Slide 13

Slide 13 text

13 Structured Concurrency: Examples All these examples can be implemented using StructuredTaskScope API.

Slide 14

Slide 14 text

14 Structured Concurrency: Continued Principle: Ø When flow of execution splits into multiple concurrent flows, they rejoin in the same code block Benefits: Ø Error handling with short-circuiting Ø Cancellation propagation Ø Clarity Ø Observability

Slide 15

Slide 15 text

Structured Concurrency API

Slide 16

Slide 16 text

16 Feature: Loan application Ø Customer applies for loan Ø Get customer details Ø Using customer details Ø Get accounts Ø Get loans Ø Get external credit scores Ø Two services provide this score Ø Response from one is enough Ø Aggregate all this info and send to offer-calculation service

Slide 17

Slide 17 text

17 Feature: Loan application Get Customer Details Get Account Details Get Loan Details Get Credit Score Calculate Offer Get Credit Score Get Credit Score 1 Get Credit Score 2

Slide 18

Slide 18 text

Scoped Values

Slide 19

Slide 19 text

ThreadLocal 19 Ø Each thread gets its own copy of the object. Ø Useful for Ø Request/session-specific data Ø Resource management (of non-thread-safe objects) Ø Caching Ø Observability (e.g. CorrelationID) Ø VirtualThread-s support ThreadLocal-s. Ø Problems Ø Unconstrained mutability Ø Unbounded lifetime Ø Expensive inheritance (with InheritableThreadLocal)

Slide 20

Slide 20 text

Scoped Values 20 Ø Immutable Ø All (allowed) threads use the same copy Ø Inherited through the StructuredTaskScope to the child-virtual-threads Ø Bounded by the scope ScopedValue VALUE = ScopedValue.newInstance(); ScopedValue.where(VALUE, someValue) .run(() -> {...});

Slide 21

Slide 21 text

Scoped Values API

Slide 22

Slide 22 text

22 Source code and contact @balarawool.bsky.social /balkrishnarawool Please rate the talk :)

Slide 23

Slide 23 text

www.ing.com Thank you!