Slide 1

Slide 1 text

Alignment indicator 29 June 2023 Balkrishna Rawool Structured Concurrency in Java The what and the why

Slide 2

Slide 2 text

2 Balkrishna Rawool IT Chapter Lead, ING Bank @BalaRawool

Slide 3

Slide 3 text

3 Project Loom Purpose: Ø Supporting easy-to-use, high-throughput lightweight concurrency and new programming models for it Features: Ø Virtual Threads: It will be finalized in Java 21 JEP - 444 Ø Structured Concurrency: Preview – JEP - 453 Ø Scoped Values: Preview – JEP - 446

Slide 4

Slide 4 text

4 (Platform) Threads JVM OS Platform Threads Platform Threads: Ø Abstraction over OS threads Ø Limited scalability (when using thread-per-request model) Ø Need for pooling

Slide 5

Slide 5 text

5 Virtual Threads JVM OS Platform Threads Virtual Threads: Ø Lightweight user threads Ø Highly scalable Ø No need for pooling Virtual Threads

Slide 6

Slide 6 text

Alignment indicator How to create virtual threads? Demo

Slide 7

Slide 7 text

7 Virtual Thread Scheduler Platform Thread Virtual Threads

Slide 8

Slide 8 text

8 Virtual Threads continued… PT 1 VT 1 VT 2 VT 3 VT 4 VT 1 VT 1 VT 2 VT 2 VT 3 VT 3 VT 4 VT 4 VT 1 VT 2 VT 3 VT 1 VT 2 VT 4 VT 3 VT 4

Slide 9

Slide 9 text

9 Use virtual threads to model (business) tasks.

Slide 10

Slide 10 text

10 Virtual threads enable Structured Concurrency

Slide 11

Slide 11 text

11 Programs → Structured Programming Concurrent programs → Structured Concurrency

Slide 12

Slide 12 text

12 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 13

Slide 13 text

13 Structured Concurrency: Examples All these can be modelled using StructuredTaskScope.

Slide 14

Slide 14 text

14 CompletableFuture Ø Provides API for asynchronous processing Ø Chain multiple stages to create pipeline Ø Callbacks

Slide 15

Slide 15 text

Alignment indicator CompletableFuture API vs Structured Concurrency API Demo

Slide 16

Slide 16 text

16 Use case 1: Event Management Reserve Venue Book Hotel Buy Supplies

Slide 17

Slide 17 text

17 Use case 2: Weather Service Get Weather From Source 1 Get Weather From Source 2 Get Weather From Source 3

Slide 18

Slide 18 text

18 Use case 1: Banking Portal Get Savings Data Get Loans Data Get Customer Data Calculate Offer

Slide 19

Slide 19 text

Shutdown policies Ø ShutdownOnFailure Stops when one of the tasks “fail” Cancels other tasks Useful when you want that all tasks must complete successfully Ø ShutdownOnSuccess Stops when one of the tasks “succeed” Cancels other tasks Useful when you want any one of the tasks to complete successfully Ø Custom Extend StructuredTaskScope Override handleComplete() Useful when you want custom logic/ some tasks to complete successfully 19

Slide 20

Slide 20 text

Be aware of Pinning! 20 Ø When virtual thread is executing a synchronized code-block or method, it gets pinned Ø Problem if you’re doing long-running/ blocking operations inside synchronized Ø Identify cases with JFR (JDK Flight Recorder) Ø Use re-entrant lock

Slide 21

Slide 21 text

Join us at 21 ing.jobs/tech

Slide 22

Slide 22 text

Questions and Feedback 22 ? Contact points: @ [email protected] @BalaRawool in/balkrishnarawool /balkrishnarawool

Slide 23

Slide 23 text

No content