-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathvirtual-threads.yaml
More file actions
49 lines (49 loc) · 1.55 KB
/
virtual-threads.yaml
File metadata and controls
49 lines (49 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
---
id: 46
slug: "virtual-threads"
title: "Virtual threads"
category: "concurrency"
difficulty: "beginner"
jdkVersion: "21"
oldLabel: "Java 8"
modernLabel: "Java 21+"
oldApproach: "Platform Threads"
modernApproach: "Virtual Threads"
oldCode: |-
Thread thread = new Thread(() -> {
System.out.println("hello");
});
thread.start();
thread.join();
modernCode: |-
Thread.startVirtualThread(() -> {
IO.println("hello");
}).join();
summary: "Create millions of lightweight virtual threads instead of heavy OS threads."
explanation: "Virtual threads are lightweight threads managed by the JVM, not the\
\ OS. You can create millions of them without tuning thread pools. They're ideal\
\ for I/O-bound tasks like HTTP calls and database queries."
whyModernWins:
- icon: "⚡"
title: "Lightweight"
desc: "Virtual threads use KB of memory, platform threads use MB."
- icon: "♾️"
title: "Scalable"
desc: "Create millions of threads — no pool sizing needed."
- icon: "🧹"
title: "Simple model"
desc: "Write blocking code that scales like async code."
support:
state: "available"
description: "Widely available since JDK 21 LTS (Sept 2023)"
prev: "streams/optional-or"
next: "concurrency/structured-concurrency"
related:
- "concurrency/thread-sleep-duration"
- "concurrency/scoped-values"
- "concurrency/completablefuture-chaining"
docs:
- title: "Virtual Threads (JEP 444)"
href: "https://openjdk.org/jeps/444"
- title: "Thread.ofVirtual()"
href: "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/Thread.html#ofVirtual()"