Slide 1

Slide 1 text

Developing Async Applications with Virtual Threads and Structured Concurrency The Loom Project José Paumard Java Developer Advocate Java Platform Group Billy Korando Senior Developer Advocate Java Platform Group

Slide 2

Slide 2 text

@billykorando.bsky.social [email protected] https://dev.java

Slide 3

Slide 3 text

@josepaumard.bsky.social https://dev.java

Slide 4

Slide 4 text

3/21/2025 Copyright © 2025, Oracle and/or its affiliates 4 https://dev.java/

Slide 5

Slide 5 text

3/21/2025 Copyright © 2025, Oracle and/or its affiliates 5 Tune in! Inside Java Newscast JEP Café Road To 21 series Inside.java Inside Java Podcast Sip of Java Cracking the Java coding interview

Slide 6

Slide 6 text

https://marketplace.eclipse.org/content/java-24-support-eclipse-2025-03-435

Slide 7

Slide 7 text

https://www.jetbrains.com/idea/download/

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

3/21/2025 Copyright © 2025, Oracle and/or its affiliates 10 https://bit.ly/javaone25-loom-lab javaone25 javaturns30

Slide 11

Slide 11 text

3/21/2025 Copyright © 2025, Oracle and/or its affiliates 11 Structure of the Lab

Slide 12

Slide 12 text

3/21/2025 Copyright © 2025, Oracle and/or its affiliates 12 Is it new? First ideas emerged in the early 60’s First specification for Fortran 1997 Structured Concurrency

Slide 13

Slide 13 text

3/21/2025 Copyright © 2025, Oracle and/or its affiliates 13 Because it is about launching different tasks in different threads with an imperative programming model But… you need to be able to create threads on demand to implement it Made possible with virtual threads! Why is This Problem Interesting?

Slide 14

Slide 14 text

3/21/2025 Copyright © 2025, Oracle and/or its affiliates 14 Stuctured Concurrency Pattern try (var scope = StructuredTaskScope.open()) { var linksSubTask = scope.fork(SomeService::readLinks); var imageSubTask = scope.fork(SomeService::readImages); scope.join(); var links = linksSubTask.get(); var images = imageSubTask.get(); var report = new Report(links, images); return report; }

Slide 15

Slide 15 text

3/21/2025 Copyright © 2025, Oracle and/or its affiliates 15 Closing ReportScope closes the other scopes A Scope can Spawn more Scopes ReportScope ImageScope LinkScope

Slide 16

Slide 16 text

3/21/2025 Copyright © 2025, Oracle and/or its affiliates 16 You get a legacy application that is a Travel Agency web application that sells travels And that wants to display the weather forecast 3 servers: - A Flight Company server - A Weather forecast server - Your Travel Agency server Goal of the lab

Slide 17

Slide 17 text

3/21/2025 Copyright © 2025, Oracle and/or its affiliates 17 This is the current state of the application And it is terrible! Some refactoring needs to be done It uses the Data Oriented Programming approach Structure of the Lab: Step 0

Slide 18

Slide 18 text

Step 0: get to know the application and refactor it 3/21/2025 Copyright © 2025, Oracle and/or its affiliates 18 The Travel Agency Application Basic Flight Company query Basic Weather forecast query Basic combination of Flight and Weather forecast

Slide 19

Slide 19 text

Step 1 and 2 3/21/2025 Copyright © 2025, Oracle and/or its affiliates 19 The Travel Agency Application Flight Company query Get all the flights Select the cheapest one Basic Weather forecast query Basic combination of Flight and Weather forecast

Slide 20

Slide 20 text

Step 3 Step 1 and 2 3/21/2025 Copyright © 2025, Oracle and/or its affiliates 20 The Travel Agency Application Flight Company query Get all the flights Select the cheapest one Weather forecast query Get the first Cancel the others Basic combination of Flight and Weather forecast

Slide 21

Slide 21 text

Step 4 and 5 Step 3 Step 1 and 2 3/21/2025 Copyright © 2025, Oracle and/or its affiliates 21 The Travel Agency Application Travel Agency application Get the cheapest flight Check if the weather forecast is there If not, cancel it Weather forecast query Get the first Cancel the others Flight Company query Get all the flights Select the cheapest one