Slide 1

Slide 1 text

JDK 21 LTS Virtual Threads a Different Approach to Async/Await

Slide 2

Slide 2 text

Aims to Bring easy-to-use, high-throughput, lightweight concurrency and new programming models on the Java platform. JDK 21 LTS

Slide 3

Slide 3 text

JDK 21: Virtual Threads A different Approach to asynchronous programming C#, Python, Javascript, C++, Rust async/await Introduced New Keywords requiring a significant rewrite to take advantage of this model choose code compatibility minimizing the impact on existing code to benefit from this model

Slide 4

Slide 4 text

JDK 21: Threads Platform Threads Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. // create a platform thread, and use .start() to start it. Thread thread = new Thread(Runnable, "thread-name") thread.start(); // create a platform thread, and use .start() to start it. Thread thread = Thread.ofPlatform().name("thread-name").unstarted(Runnable); thread.start(); // create a platform thread and start it. No need to call .start() Thread thread = Thread.ofPlatform().name("thread-name").start(Runnable);

Slide 5

Slide 5 text

JDK 21: Threads Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Platform Threads & Executor Services

Slide 6

Slide 6 text

JDK 21: Threads Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs try (ExecutorService executor = Executors.newFixedThreadPool(4)) { for (int i = 0; i < 100; ++i) { executor.submit(() -> myTask()); } } Platform Threads & Executor Services

Slide 7

Slide 7 text

JDK 21: Threads Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue Platform Threads & Executor Services try (ExecutorService executor = Executors.newFixedThreadPool(4)) { for (int i = 0; i < 100; ++i) { executor.submit(() -> myTask()); } }

Slide 8

Slide 8 text

JDK 21: Threads Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue Platform Threads & Executor Services try (ExecutorService executor = Executors.newFixedThreadPool(4)) { for (int i = 0; i < 100; ++i) { executor.submit(() -> myTask()); } }

Slide 9

Slide 9 text

JDK 21: Threads Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue Platform Threads & Executor Services try (ExecutorService executor = Executors.newFixedThreadPool(4)) { for (int i = 0; i < 100; ++i) { executor.submit(() -> myTask()); } }

Slide 10

Slide 10 text

JDK 21: Threads Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue Platform Threads & Executor Services try (ExecutorService executor = Executors.newFixedThreadPool(4)) { for (int i = 0; i < 100; ++i) { executor.submit(() -> myTask()); } }

Slide 11

Slide 11 text

JDK 21: Threads Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue Platform Threads & Executor Services try (ExecutorService executor = Executors.newFixedThreadPool(4)) { for (int i = 0; i < 100; ++i) { executor.submit(() -> myTask()); } }

Slide 12

Slide 12 text

JDK 21: Threads Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue Platform Threads & Executor Services try (ExecutorService executor = Executors.newFixedThreadPool(4)) { for (int i = 0; i < 100; ++i) { executor.submit(() -> myTask()); } }

Slide 13

Slide 13 text

JDK 21: Threads Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue Platform Threads & Executor Services try (ExecutorService executor = Executors.newFixedThreadPool(4)) { for (int i = 0; i < 100; ++i) { executor.submit(() -> myTask()); } }

Slide 14

Slide 14 text

JDK 21: Threads Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue Platform Threads & Executor Services try (ExecutorService executor = Executors.newFixedThreadPool(4)) { for (int i = 0; i < 100; ++i) { executor.submit(() -> myTask()); } }

Slide 15

Slide 15 text

JDK 21: Threads Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue Platform Threads & Executor Services try (ExecutorService executor = Executors.newFixedThreadPool(4)) { for (int i = 0; i < 100; ++i) { executor.submit(() -> myTask()); } }

Slide 16

Slide 16 text

JDK 21: Threads Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue Platform Threads & Executor Services try (ExecutorService executor = Executors.newFixedThreadPool(4)) { for (int i = 0; i < 100; ++i) { executor.submit(() -> myTask()); } }

Slide 17

Slide 17 text

JDK 21: Threads Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue Platform Threads & Executor Services try (ExecutorService executor = Executors.newFixedThreadPool(4)) { for (int i = 0; i < 100; ++i) { executor.submit(() -> myTask()); } }

Slide 18

Slide 18 text

JDK 21: Threads Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue Platform Threads & Executor Services try (ExecutorService executor = Executors.newFixedThreadPool(4)) { for (int i = 0; i < 100; ++i) { executor.submit(() -> myTask()); } }

Slide 19

Slide 19 text

JDK 21: Threads Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue Platform Threads & Executor Services try (ExecutorService executor = Executors.newFixedThreadPool(4)) { for (int i = 0; i < 100; ++i) { executor.submit(() -> myTask()); } }

Slide 20

Slide 20 text

JDK 21: Threads Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue Platform Threads & Executor Services try (ExecutorService executor = Executors.newFixedThreadPool(4)) { for (int i = 0; i < 100; ++i) { executor.submit(() -> myTask()); } }

Slide 21

Slide 21 text

JDK 21: Threads Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue Platform Threads & Executor Services try (ExecutorService executor = Executors.newFixedThreadPool(4)) { for (int i = 0; i < 100; ++i) { executor.submit(() -> myTask()); } }

Slide 22

Slide 22 text

JDK 21: Threads Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue Platform Threads & Executor Services try (ExecutorService executor = Executors.newFixedThreadPool(4)) { for (int i = 0; i < 100; ++i) { executor.submit(() -> myTask()); } }

Slide 23

Slide 23 text

JDK 21: Threads Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue Platform Threads & Executor Services try (ExecutorService executor = Executors.newFixedThreadPool(4)) { for (int i = 0; i < 100; ++i) { executor.submit(() -> myTask()); } }

Slide 24

Slide 24 text

JDK 21: Threads Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue Platform Threads & Executor Services try (ExecutorService executor = Executors.newFixedThreadPool(4)) { for (int i = 0; i < 100; ++i) { executor.submit(() -> myTask()); } }

Slide 25

Slide 25 text

JDK 21: Threads Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue Platform Threads & Executor Services try (ExecutorService executor = Executors.newFixedThreadPool(4)) { for (int i = 0; i < 100; ++i) { executor.submit(() -> myTask()); } }

Slide 26

Slide 26 text

JDK 21: Executor Services Slow Tasks & Completable Future Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue

Slide 27

Slide 27 text

JDK 21: Executor Services Slow Tasks & Completable Future Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue

Slide 28

Slide 28 text

JDK 21: Executor Services Slow Tasks & Completable Future Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue

Slide 29

Slide 29 text

JDK 21: Executor Services Slow Tasks & Completable Future Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue

Slide 30

Slide 30 text

JDK 21: Executor Services Slow Tasks & Completable Future Kernel space User space (JVM) Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs Task Queue

Slide 31

Slide 31 text

JDK 21: Executor Services Slow Tasks & Completable Future Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs In most backend services Slow Tasks are I/O Bound Tasks

Slide 32

Slide 32 text

JDK 21: Executor Services Slow Tasks & Completable Future Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs System.out.println("Active Threads " + Thread.activeCount()); for (int i = 0; i < 128; ++i) { httpClient.sendAsync(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); System.out.println("Active Threads " + Thread.activeCount()); } CompletableFuture and the Async feeling

Slide 33

Slide 33 text

JDK 21: Executor Services Slow Tasks & Completable Future Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs System.out.println("Active Threads " + Thread.activeCount()); for (int i = 0; i < 128; ++i) { httpClient.sendAsync(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); System.out.println("Active Threads " + Thread.activeCount()); } CompletableFuture and the Async feeling My Task Not being blocked

Slide 34

Slide 34 text

JDK 21: Executor Services Slow Tasks & Completable Future Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs System.out.println("Active Threads " + Thread.activeCount()); for (int i = 0; i < 128; ++i) { httpClient.sendAsync(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); System.out.println("Active Threads " + Thread.activeCount()); } CompletableFuture and the Async feeling All the threads created by .sendAsync() Waiting for I/O My Task Not being blocked

Slide 35

Slide 35 text

JDK 21: Executor Services Slow Tasks & Completable Future Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs System.out.println("Active Threads " + Thread.activeCount()); for (int i = 0; i < 128; ++i) { httpClient.sendAsync(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); System.out.println("Active Threads " + Thread.activeCount()); } CompletableFuture and the Async feeling All the threads created by .sendAsync() Waiting for I/O My Task Not being blocked

Slide 36

Slide 36 text

JDK 21: Executor Services Slow Tasks & Completable Future Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs System.out.println("Active Threads " + Thread.activeCount()); for (int i = 0; i < 128; ++i) { httpClient.sendAsync(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); System.out.println("Active Threads " + Thread.activeCount()); } CompletableFuture and the Async feeling All the threads created by .sendAsync() Waiting for I/O My Task Not being blocked

Slide 37

Slide 37 text

JDK 21: Executor Services Slow Tasks & Completable Future Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs System.out.println("Active Threads " + Thread.activeCount()); for (int i = 0; i < 128; ++i) { httpClient.sendAsync(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); System.out.println("Active Threads " + Thread.activeCount()); } CompletableFuture and the Async feeling All the threads created by .sendAsync() Waiting for I/O My Task Not being blocked

Slide 38

Slide 38 text

JDK 21: Executor Services Slow Tasks & Completable Future Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs System.out.println("Active Threads " + Thread.activeCount()); for (int i = 0; i < 128; ++i) { httpClient.sendAsync(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); System.out.println("Active Threads " + Thread.activeCount()); } CompletableFuture and the Async feeling All the threads created by .sendAsync() Waiting for I/O My Task Not being blocked

Slide 39

Slide 39 text

JDK 21: Executor Services Slow Tasks & Completable Future Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs System.out.println("Active Threads " + Thread.activeCount()); for (int i = 0; i < 128; ++i) { httpClient.sendAsync(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); System.out.println("Active Threads " + Thread.activeCount()); } CompletableFuture and the Async feeling All the threads created by .sendAsync() Waiting for I/O My Task Not being blocked

Slide 40

Slide 40 text

JDK 21: Executor Services Slow Tasks & Completable Future Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs System.out.println("Active Threads " + Thread.activeCount()); for (int i = 0; i < 128; ++i) { httpClient.sendAsync(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); System.out.println("Active Threads " + Thread.activeCount()); } CompletableFuture and the Async feeling All the threads created by .sendAsync() Waiting for I/O My Task Not being blocked

Slide 41

Slide 41 text

JDK 21: Executor Services Slow Tasks & Completable Future Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs System.out.println("Active Threads " + Thread.activeCount()); for (int i = 0; i < 128; ++i) { httpClient.sendAsync(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); System.out.println("Active Threads " + Thread.activeCount()); } CompletableFuture and the Async feeling All the threads created by .sendAsync() Waiting for I/O My Task Not being blocked

Slide 42

Slide 42 text

JDK 21: Executor Services Slow Tasks & Completable Future Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Platform Threads are not cheap - startup time: ~1ms - memory consumption: 2MB of stack - context switch: ~100μs System.out.println("Active Threads " + Thread.activeCount()); for (int i = 0; i < 128; ++i) { httpClient.sendAsync(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); System.out.println("Active Threads " + Thread.activeCount()); } CompletableFuture and the Async feeling All the threads created by .sendAsync() Waiting for I/O My Task Not being blocked

Slide 43

Slide 43 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. JDK 21: Threads Virtual Threads User space (JVM) // create a virtual thread and start it. Thread thread = Thread.startVirtualThread(Runnable); // create a virtual thread and start it. Thread thread = Thread.ofVirtual().name(“thread-name").start(Runnable); // create a virtual thread, and use .start() to start it. Thread thread = Thread.ofVirtual().name("thread-name").unstarted(Runnable); thread.start(); Virtual Threads JVM

Slide 44

Slide 44 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. JDK 21: Threads Virtual Threads Eliminates the overhead of I/O Bound Tasks

Slide 45

Slide 45 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. JDK 21: Threads Virtual Threads User space (JVM) Virtual Threads JVM

Slide 46

Slide 46 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. JDK 21: Threads Virtual Threads User space (JVM) Virtual Threads JVM Use them for I/O Bound Tasks

Slide 47

Slide 47 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. JDK 21: Threads Virtual Threads User space (JVM) Virtual Threads JVM Use them for I/O Bound Tasks Spends most of the time using CPU Spends most of the time waiting for I/O Longer CPU bursts Shorter CPU bursts CPU Bound I/O Bound

Slide 48

Slide 48 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. JDK 21: Threads Virtual Threads User space (JVM) Virtual Threads JVM Almost no changes to the code required! Java did all of the work underneath Rewriting the I/O & Blocking Operations To Cooperate with the Virtual Threads Scheduler

Slide 49

Slide 49 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. JDK 21: Threads Virtual Threads User space (JVM) try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { executor.submit(() -> myTask()); } Virtual Threads JVM

Slide 50

Slide 50 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. JDK 21: Threads Virtual Threads User space (JVM) // create a virtual thread and start it. Thread thread = Thread.startVirtualThread(Runnable); // create a virtual thread and start it. Thread thread = Thread.ofVirtual().name(“thread-name").start(Runnable); // create a virtual thread, and use .start() to start it. Thread thread = Thread.ofVirtual().name("thread-name").unstarted(Runnable); thread.start(); Virtual Threads JVM

Slide 51

Slide 51 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. JDK 21: Threads Virtual Threads // create a platform thread and start it. No need to call .start() Thread thread = Thread.ofPlatform().name(“my-thread-name”).start(Runnable); // create a virtual thread and start it. Thread thread = Thread.ofVirtual().name(“my-thread-name”).start(Runnable); boolean isVirtual = thread.isVirtual();

Slide 52

Slide 52 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. JDK 21: Threads Virtual Threads // create a platform thread and start it. No need to call .start() Thread thread = Thread.ofPlatform().name(“my-thread-name”).start(Runnable); // create a virtual thread and start it. Thread thread = Thread.ofVirtual().name(“my-thread-name”).start(Runnable); boolean isVirtual = thread.isVirtual(); Thread[#29,my-thread-name,5,main] VirtualThread[#30,my-thread-name]/runnable@ForkJoinPool-1-worker-1 jstack or Thread.currentThread() shows

Slide 53

Slide 53 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. JDK 21: Threads Virtual Threads // create a platform thread and start it. No need to call .start() Thread thread = Thread.ofPlatform().name(“my-thread-name”).start(Runnable); // create a virtual thread and start it. Thread thread = Thread.ofVirtual().name(“my-thread-name”).start(Runnable); boolean isVirtual = thread.isVirtual(); Thread[#29,my-thread-name,5,main] VirtualThread[#30,my-thread-name]/runnable@ForkJoinPool-1-worker-1 jstack or Thread.currentThread() shows

Slide 54

Slide 54 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Kernel space User space (JVM) Virtual Threads Carrier Threads

Slide 55

Slide 55 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Kernel space User space (JVM) Virtual Threads Carrier Threads ForkJoinPool with FIFO order

Slide 56

Slide 56 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Kernel space User space (JVM) Virtual Threads Carrier Threads ForkJoinPool with FIFO order -Djdk.virtualThreadScheduler.parallelism=16 -Djdk.virtualThreadScheduler.maxPoolSize=256 The number of platform threads available for scheduling virtual threads. It defaults to the number of available processors. The maximum number of platform threads available to the scheduler. It defaults to 256.

Slide 57

Slide 57 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread

Slide 58

Slide 58 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ...

Slide 59

Slide 59 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ...

Slide 60

Slide 60 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ...

Slide 61

Slide 61 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ...

Slide 62

Slide 62 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ...

Slide 63

Slide 63 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ... Waiting Tasks

Slide 64

Slide 64 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ... Waiting Tasks JVM Heap

Slide 65

Slide 65 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ... Waiting Tasks JVM Heap Task State

Slide 66

Slide 66 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ... Waiting Tasks JVM Heap Task State

Slide 67

Slide 67 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ... Waiting Tasks JVM Heap Task State

Slide 68

Slide 68 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ... Waiting Tasks JVM Heap Task State

Slide 69

Slide 69 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ... Waiting Tasks JVM Heap Task State

Slide 70

Slide 70 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ... Waiting Tasks JVM Heap Task State

Slide 71

Slide 71 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ... Waiting Tasks JVM Heap Task State

Slide 72

Slide 72 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ... Waiting Tasks JVM Heap Task State

Slide 73

Slide 73 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ... Waiting Tasks JVM Heap Task State

Slide 74

Slide 74 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ... Waiting Tasks JVM Heap Task State

Slide 75

Slide 75 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ... Waiting Tasks JVM Heap Task State

Slide 76

Slide 76 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ... Waiting Tasks JVM Heap Task State

Slide 77

Slide 77 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ... Waiting Tasks JVM Heap Task State

Slide 78

Slide 78 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ... Waiting Tasks JVM Heap Task State

Slide 79

Slide 79 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ... Waiting Tasks JVM Heap Task State

Slide 80

Slide 80 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ... Waiting Tasks JVM Heap Task State

Slide 81

Slide 81 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ... Waiting Tasks JVM Heap Task State

Slide 82

Slide 82 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ... Waiting Tasks JVM Heap Task State

Slide 83

Slide 83 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ... Waiting Tasks JVM Heap

Slide 84

Slide 84 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads Virtual Threads Carrier Thread // some cpu bound code long result = 0; for (int i = 0; i < 100; ++i) { result += i * 3; } // some blocking (I/O bound) code HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create(“https://myservice/test”)) .GET() .build(), BodyHandlers.discarding()); // more cpu bound code if (resp.statusCode() != 200) { ... Waiting Tasks JVM Heap

Slide 85

Slide 85 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads public void myTask() { // each execution step may run on a different Carrier Thraed cpuBoundOperations(); // run on CarrierThread-5 ioBoundOperations(); // goes to "waiting" cpuBoundOperations(); // run on CarrierThread-3 ioBoundOperations(); // goes to "waiting" cpuBoundOperations(); // run on CarrierThread-3 // ... }

Slide 86

Slide 86 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads public void myTask() { // each execution step may run on a different Carrier Thraed cpuBoundOperations(); // run on CarrierThread-5 ioBoundOperations(); // goes to "waiting" cpuBoundOperations(); // run on CarrierThread-3 ioBoundOperations(); // goes to "waiting" cpuBoundOperations(); // run on CarrierThread-3 // ... } ThreadLocals are available for compatibility but not really useful. Do not pool VirtualThreads!

Slide 87

Slide 87 text

Virtual Threads User-mode threads scheduled by the Java Runtime rather than the operating system Platform Threads Typically mapped 1:1 to kernel threads scheduled by the operating system. Carrier Threads Set of Platform threads responsible for running virtual threads. JDK 21: Threads Platform, Virtual & Carrier Threads public void myTask() { // each execution step may run on a different Carrier Thraed cpuBoundOperations(); // run on CarrierThread-5 ioBoundOperations(); // goes to "waiting" cpuBoundOperations(); // run on CarrierThread-3 ioBoundOperations(); // goes to "waiting" cpuBoundOperations(); // run on CarrierThread-3 // ... } ThreadLocals are available for compatibility but not really useful. Do not pool VirtualThreads! ScopedValue SHARED_OBJECT = ScopedValue.newInstance(); ScopedValues, the better alternative to ThreadLocal are only available as preview (in JDK 21)

Slide 88

Slide 88 text

Code Examples Let's see how Virtual Threads behaves

Slide 89

Slide 89 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } }

Slide 90

Slide 90 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } We are using the synchronous .send() No callbacks, futures & co

Slide 91

Slide 91 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } }

Slide 92

Slide 92 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 93

Slide 93 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 94

Slide 94 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 95

Slide 95 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 96

Slide 96 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 97

Slide 97 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 98

Slide 98 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 99

Slide 99 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 100

Slide 100 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 101

Slide 101 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 102

Slide 102 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 103

Slide 103 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 104

Slide 104 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 105

Slide 105 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 106

Slide 106 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 107

Slide 107 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 108

Slide 108 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 109

Slide 109 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 110

Slide 110 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 111

Slide 111 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 112

Slide 112 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 113

Slide 113 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 114

Slide 114 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 115

Slide 115 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 116

Slide 116 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 117

Slide 117 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 118

Slide 118 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 119

Slide 119 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 120

Slide 120 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 121

Slide 121 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 122

Slide 122 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 123

Slide 123 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 124

Slide 124 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 125

Slide 125 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 126

Slide 126 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 127

Slide 127 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 128

Slide 128 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 129

Slide 129 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 130

Slide 130 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 131

Slide 131 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 132

Slide 132 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 133

Slide 133 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 134

Slide 134 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 135

Slide 135 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 136

Slide 136 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 137

Slide 137 text

JDK 21: Virtual Threads I/O Bound Tasks private static long ioBoundTask() { try { // calls a service that waits 5sec before responding… final HttpResponse resp = httpClient.send(HttpRequest.newBuilder() .uri(URI.create("https://hub.dummyapis.com/delay?seconds=5")) .GET() .build(), BodyHandlers.discarding()); // ... return resp.statusCode(); } catch (final Exception e) { // ignore... System.err.println("http req failed: " + e.getMessage()); return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks

Slide 138

Slide 138 text

JDK 21: Virtual Threads I/O Bound Tasks private static int sleepBoundTask() { try { // sleep() acts as an I/O operation, allowing the task unmount Thread.sleep(5_000); return 0; } catch (final InterruptedException e) { // ignore... return -1; } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); final Thread[] threads = new Thread[10]; for (int i = 0; i < threads.length; ++i) { final int taskId = i; threads[i] = Thread.ofVirtual().start(() -> { sleepBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } for (final Thread thread : threads) { thread.join(); final boolean isVirtual = thread.isVirtual(); }

Slide 139

Slide 139 text

JDK 21: Virtual Threads CPU Bound Tasks Virtual Threads are made for I/O Bound Tasks Let’s see how they will behave with CPU Bound Tasks

Slide 140

Slide 140 text

JDK 21: Virtual Threads CPU Bound Tasks private static long cpuBoundTask() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { count++; } return count; } There are no blocking points in the code To Unmount the Virtual Thread

Slide 141

Slide 141 text

JDK 21: Virtual Threads CPU Bound Tasks private static long cpuBoundTask() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { count++; } return count; } // Run with -Djdk.virtualThreadScheduler.parallelism=2 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Threads

Slide 142

Slide 142 text

JDK 21: Virtual Threads CPU Bound Tasks private static long cpuBoundTask() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { count++; } return count; } // Run with -Djdk.virtualThreadScheduler.parallelism=2 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Threads

Slide 143

Slide 143 text

JDK 21: Virtual Threads CPU Bound Tasks private static long cpuBoundTask() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { count++; } return count; } // Run with -Djdk.virtualThreadScheduler.parallelism=2 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Threads 1. Completed in 5s

Slide 144

Slide 144 text

JDK 21: Virtual Threads CPU Bound Tasks private static long cpuBoundTask() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { count++; } return count; } // Run with -Djdk.virtualThreadScheduler.parallelism=2 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Threads 1. Completed in 5s

Slide 145

Slide 145 text

JDK 21: Virtual Threads CPU Bound Tasks private static long cpuBoundTask() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { count++; } return count; } // Run with -Djdk.virtualThreadScheduler.parallelism=2 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Threads 1. Completed in 5s 2. Completed in 5s

Slide 146

Slide 146 text

JDK 21: Virtual Threads CPU Bound Tasks private static long cpuBoundTask() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { count++; } return count; } // Run with -Djdk.virtualThreadScheduler.parallelism=2 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Threads 1. Completed in 5s 2. Completed in 5s

Slide 147

Slide 147 text

JDK 21: Virtual Threads CPU Bound Tasks private static long cpuBoundTask() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { count++; } return count; } // Run with -Djdk.virtualThreadScheduler.parallelism=2 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Threads 1. Completed in 5s 2. Completed in 5s 3. Completed in 10s 4. Completed in 10s

Slide 148

Slide 148 text

JDK 21: Virtual Threads CPU Bound Tasks private static long cpuBoundTask() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { count++; } return count; } // Run with -Djdk.virtualThreadScheduler.parallelism=2 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Threads 1. Completed in 5s 2. Completed in 5s 3. Completed in 10s 4. Completed in 10s

Slide 149

Slide 149 text

JDK 21: Virtual Threads CPU Bound Tasks private static long cpuBoundTask() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { count++; } return count; } // Run with -Djdk.virtualThreadScheduler.parallelism=2 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Threads 1. Completed in 5s 2. Completed in 5s 3. Completed in 10s 4. Completed in 10s 5. Completed in 15s 6. Completed in 15s

Slide 150

Slide 150 text

JDK 21: Virtual Threads CPU Bound Tasks private static long cpuBoundTask() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { count++; } return count; } // Run with -Djdk.virtualThreadScheduler.parallelism=2 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Threads 1. Completed in 5s 2. Completed in 5s 3. Completed in 10s 4. Completed in 10s 5. Completed in 15s 6. Completed in 15s

Slide 151

Slide 151 text

JDK 21: Virtual Threads CPU Bound Tasks private static long cpuBoundTask() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { count++; } return count; } // Run with -Djdk.virtualThreadScheduler.parallelism=2 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Threads 1. Completed in 5s 2. Completed in 5s 3. Completed in 10s 4. Completed in 10s 5. Completed in 15s 6. Completed in 15s 7. Completed in 20s 8. Completed in 20s

Slide 152

Slide 152 text

JDK 21: Virtual Threads CPU Bound Tasks private static long cpuBoundTask() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { count++; } return count; } // Run with -Djdk.virtualThreadScheduler.parallelism=2 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Threads 1. Completed in 5s 2. Completed in 5s 3. Completed in 10s 4. Completed in 10s 5. Completed in 15s 6. Completed in 15s 7. Completed in 20s 8. Completed in 20s

Slide 153

Slide 153 text

JDK 21: Virtual Threads CPU Bound Tasks private static long cpuBoundTask() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { count++; } return count; } // Run with -Djdk.virtualThreadScheduler.parallelism=2 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Threads 1. Completed in 5s 2. Completed in 5s 3. Completed in 10s 4. Completed in 10s 5. Completed in 15s 6. Completed in 15s 7. Completed in 20s 8. Completed in 20s 9. Completed in 25s 10.Completed in 25s

Slide 154

Slide 154 text

JDK 21: Virtual Threads CPU Bound Tasks private static long cpuBoundTask() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { count++; } return count; } // Run with -Djdk.virtualThreadScheduler.parallelism=2 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTask(); long elapsedNs = System.nanoTime() - startTime; }); } } For CPU bound Tasks Virtual Threads gives no advantage over traditional (Platform Thread based) ExecutorServices And I/O Bound Tasks Have to wait CPU Bound Tasks to Finish

Slide 155

Slide 155 text

Virtual Threads Carrier Threads CPU Bound Tasks Platform Threads Send CPU and I/O bound tasks to dedicated executors ThreadPoolExecutor VirtualThraedPerTaskExecutor JDK 21: Virtual Threads

Slide 156

Slide 156 text

JDK 21: Virtual Threads Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; } CPU Bound Tasks can cooperate Using the yield() method

Slide 157

Slide 157 text

JDK 21: Virtual Threads Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } Carrier Thread Waiting Tasks private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 158

Slide 158 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 159

Slide 159 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 160

Slide 160 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 161

Slide 161 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 162

Slide 162 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 163

Slide 163 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 164

Slide 164 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 165

Slide 165 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 166

Slide 166 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 167

Slide 167 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 168

Slide 168 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 169

Slide 169 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 170

Slide 170 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 171

Slide 171 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 172

Slide 172 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 173

Slide 173 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 174

Slide 174 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 175

Slide 175 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 176

Slide 176 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 177

Slide 177 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 178

Slide 178 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 179

Slide 179 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 180

Slide 180 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 181

Slide 181 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 182

Slide 182 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 183

Slide 183 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 184

Slide 184 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 185

Slide 185 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 186

Slide 186 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 187

Slide 187 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 188

Slide 188 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 189

Slide 189 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 190

Slide 190 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 191

Slide 191 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 192

Slide 192 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 193

Slide 193 text

JDK 21: Virtual Threads Carrier Thread Waiting Tasks Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; }

Slide 194

Slide 194 text

JDK 21: Virtual Threads Cooperative CPU Bound Tasks // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { cpuBoundTaskWithYield(); long elapsedNs = System.nanoTime() - startTime; }); } } private static long cpuBoundTaskWithYield() { final long startTime = System.nanoTime(); long count = 0; while ((System.nanoTime() - startTime) < TimeUnit.SECONDS.toNanos(5)) { if (count++ % 100 == 0) { Thread.yield(); } } return count; } Slower than running the task alone (Since the CPU is shared) But it’s the same behaviour of running multiple platform Threads

Slide 195

Slide 195 text

JDK 21: Virtual Threads The synchronized keyword private final static Object[] lock = new Object[4]; static { for (int i = 0; i < lock.length; ++i) { lock[i] = new Object(); } } private static long ioSynchronizedTask(final int taskId) { synchronized (lock[taskId % lock.length]) { return ioBoundTask(); } } synchronized will block the carrier thread

Slide 196

Slide 196 text

JDK 21: Virtual Threads The synchronized keyword private final static Object[] lock = new Object[4]; static { for (int i = 0; i < lock.length; ++i) { lock[i] = new Object(); } } private static long ioSynchronizedTask(final int taskId) { synchronized (lock[taskId % lock.length]) { return ioBoundTask(); } } Multiple synchronized Object Will not help If you are lucky You’ll get the CPU Bound Task Behaviour Even on I/O Operations

Slide 197

Slide 197 text

JDK 21: Virtual Threads The synchronized keyword private final static Object[] lock = new Object[4]; static { for (int i = 0; i < lock.length; ++i) { lock[i] = new Object(); } } private static long ioSynchronizedTask(final int taskId) { synchronized (lock[taskId % lock.length]) { return ioBoundTask(); } } Remove the use of Synchronized

Slide 198

Slide 198 text

JDK 21: Virtual Threads Locks: Reentrant Lock, Semaphores, … private static final ReentrantLock[] rlock = new ReentrantLock[4]; static { for (int i = 0; i < rlock.length; ++i) { rlock[i] = new ReentrantLock(); } } private static long ioLockedTask(final int taskId) { final ReentrantLock lock = rlock[taskId % rlock.length]; lock.lock(); try { return ioBoundTask(); // http.send(), sleep(), ... } finally { lock.unlock(); } } // Run with -Djdk.virtualThreadScheduler.parallelism=1 final long startTime = System.nanoTime(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { for (int i = 0; i < 10; ++i) { final int taskId = i; executor.submit(() -> { ioLockedTask(); long elapsedNs = System.nanoTime() - startTime; }); } } 4 Tasks will run concurrently Locks are a blocking operation Releasing the Virtual Thread from the Carrier

Slide 199

Slide 199 text

JDK 21 Virtual Threads Do not go and replace all your ExecutorServices with Virtual Threads! CPU Bound Tasks will not benefit from it Virtual Threads are made for I/O bound Tasks Spends most of the time using CPU Spends most of the time waiting for I/O Longer CPU bursts Shorter CPU bursts CPU Bound I/O Bound

Slide 200

Slide 200 text

JDK 21 Virtual Threads Do not go and replace all your ExecutorServices with Virtual Threads! CPU Bound Tasks will not benefit from it Virtual Threads are made for I/O bound Tasks Spends most of the time using CPU Spends most of the time waiting for I/O Longer CPU bursts Shorter CPU bursts CPU Bound I/O Bound Virtual Threads A Service or a Database

Slide 201

Slide 201 text

JDK 21 Virtual Threads Do not go and replace all your ExecutorServices with Virtual Threads! CPU Bound Tasks will not benefit from it Virtual Threads are made for I/O bound Tasks Spends most of the time using CPU Spends most of the time waiting for I/O Longer CPU bursts Shorter CPU bursts CPU Bound I/O Bound Virtual Threads A Service or a Database Can your Database, Service, Disk, … handle the extra concurrency? (Probably not) If you are using Virtual Threads You should limit the access To a Resource Using a Semaphore (or similar)