Slide 1

Slide 1 text

Alignment indicator 10 Jan 2024 Balkrishna Rawool Continuations The magic behind virtual threads

Slide 2

Slide 2 text

2 Balkrishna Rawool IT Chapter Lead, Business Lending @BalaRawool

Slide 3

Slide 3 text

Alignment indicator Virtual Threads

Slide 4

Slide 4 text

4 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 5

Slide 5 text

5 Virtual Thread Scheduler Platform/ Carrier Thread Virtual Threads

Slide 6

Slide 6 text

Alignment indicator How to create virtual threads?

Slide 7

Slide 7 text

Alignment indicator Continuations

Slide 8

Slide 8 text

Continuation 8 Ø A continuation can be viewed as Ø a representation of the current state of the program

Slide 9

Slide 9 text

Continuation 9 Ø A continuation can be viewed as Ø a representation of the current state of the program or Ø ‘the rest of the computation’ Ø It allows us to pause execution of a program (or part thereof) and resume it later. Ø WARNING: Continuation API in Java is not supposed to be used directly by application developers. For now, it is only used internally to implement virtual threads.

Slide 10

Slide 10 text

Alignment indicator Continuations in Java

Slide 11

Slide 11 text

11 Continuations in Java are One Shot Multi-Prompt Delimited Continuations

Slide 12

Slide 12 text

Continuations in Java 12 Continuation.yield() yield() d() c() b() run() a() main() Stack Heap d() c() b() run() a() main() Stack Heap yield()

Slide 13

Slide 13 text

Continuations in Java 13 Continuation.run() c() b() run() a() main() Stack Heap d() c() b() run() a() main() Stack Heap yield() yield() d()

Slide 14

Slide 14 text

Uses of continuations 14 Ø Green threads or virtual threads Ø Coroutines Ø Exception handling Ø Generators

Slide 15

Slide 15 text

Generator 15 Ø A generator allows us to write a function that ‘yields’ values. Ø These values are lazily retrieved. Ø They can then be iterated over by using a loop. { //… //… yield 1; //… //… yield 2; //… //… yield 3; //… //… yield 4; } Generator while () { // … } Consumer

Slide 16

Slide 16 text

Alignment indicator Generator using Continuations

Slide 17

Slide 17 text

Alignment indicator A Simple VirtualThread using Continuations

Slide 18

Slide 18 text

Simple VirtualThread 18 Ø Goals Ø A VirtualThread instance should not use CPU (or platform thread) when waiting/ blocking. Ø A VirtualThread can get mounted on several platform threads during its lifetime. Ø A platform thread can support several VirtualThread-s during its lifetime.

Slide 19

Slide 19 text

Simple VirtualThread 19 VirtualThread VirtualThreadScheduler Virtual threads Platform threads // Consumer { // … // Blocking call // … }

Slide 20

Slide 20 text

Source code 20 github.com/balkrishnarawool

Slide 21

Slide 21 text

Connect with me at 21 @BalaRawool

Slide 22

Slide 22 text

No content