Slide 1

Slide 1 text

30 May 2024 Balkrishna Rawool Continuations The magic behind virtual threads

Slide 2

Slide 2 text

2 Balkrishna Rawool IT Chapter Lead, ING Bank @BalaRawool

Slide 3

Slide 3 text

Introduction to Virtual Threads

Slide 4

Slide 4 text

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

Slide 5 text

5 Virtual Thread Scheduler Platform/ Carrier Thread Virtual Threads

Slide 6

Slide 6 text

How to create virtual threads?

Slide 7

Slide 7 text

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. Ø Continuations in Java Ø 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

Continuations in Java

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Uses of continuations 14 Ø Virtual threads or green threads Ø Coroutines Ø Exception handling Ø Generators

Slide 14

Slide 14 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 “A”; //… //… yield “B”; //… //… yield “C”; } Generator while () { // … } Consumer

Slide 15

Slide 15 text

Generator using Continuations

Slide 16

Slide 16 text

A Simple VirtualThread using Continuations

Slide 17

Slide 17 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 18

Slide 18 text

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

Slide 19

Slide 19 text

Source code and contact 20 @BalaRawool

Slide 20

Slide 20 text

No content