-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathVirtualThreadsContinuations.java
196 lines (166 loc) · 5.48 KB
/
VirtualThreadsContinuations.java
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.Timer;
import java.util.TimerTask;
import jdk.internal.vm.Continuation;
import jdk.internal.vm.ContinuationScope;
/**
* Examples from "Continuations: The magic behind virtual threads in Java by Balkrishna Rawool @ Spring I/O 2024"
*
* Run:
* ```bash
* javac --enable-preview --source 21 --target 21 --add-exports java.base/jdk.internal.vm=ALL-UNNAMED VirtualThreadsContinuations.java
* java --enable-preview --add-exports java.base/jdk.internal.vm=ALL-UNNAMED VirtualThreadsContinuations
* ```
*/
public class VirtualThreadsContinuations {
public static void main(String[] args) {
System.out.println("=== Continuation Example ===");
basicExample();
System.out.println("\n=== Generator Example ===");
generatorExample();
System.out.println("\n=== Custom VirtualThread Example ===");
virtualThreadExample();
}
static void basicExample() {
var scope = new ContinuationScope("my-scope");
// continuation needs a scope and a task
var continuation = new Continuation(scope, () -> {
System.out.println("A");
// `yield` pauses the scope execution and returns the execution to another scope
// the execution will be paused at the point where `Continuation.yield` is called
// the stacktrace is copied to heap
// will be paused until `run` is called again
Continuation.yield(scope);
System.out.println("B");
Continuation.yield(scope);
System.out.println("C");
});
System.out.println("Starting continuation");
continuation.run();
System.out.println("Resuming continuation");
continuation.run();
continuation.run();
// if we try to run a running continuation or terminated continuation, a IllegalStateException is thrown
// continuation.run();
if (continuation.isDone()) {
System.out.println("Continuation terminated");
} else {
System.out.println("Continuation is pending");
}
}
static void generatorExample() {
var generator = new Generator<String>(source -> {
try {
Files.list(Paths.get("/"))
.forEach(path -> source.yield(path.toString()));
} catch (IOException ex) {
source.yield("Error: " + ex.getMessage());
}
});
while (generator.hasNext()) {
System.out.println("Generated: " + generator.next());
}
}
static void virtualThreadExample() {
// start the scheduler in a new thread just for it
new Thread(VirtualThreadScheduler.SCHEDULER::start).start();
var vt1 = new VirtualThread(() -> {
System.out.println("1.1");
System.out.println("1.2");
WaitingOperation.perform("Network", 2);
System.out.println("1.3");
System.out.println("1.4");
});
var vt2 = new VirtualThread(() -> {
System.out.println("2.1");
System.out.println("2.2");
WaitingOperation.perform("Disk", 2);
System.out.println("2.3");
System.out.println("2.4");
});
VirtualThreadScheduler.SCHEDULER.schedule(vt1);
VirtualThreadScheduler.SCHEDULER.schedule(vt2);
}
}
class Generator<T> {
private ContinuationScope scope;
private Continuation continuation;
private Source source;
public class Source {
private T value;
public T getValue() {
var v = this.value;
continuation.run();
return v;
}
public void yield(T value) {
this.value = value;
Continuation.yield(scope);
}
}
public Generator(Consumer<Source> consumer) {
this.scope = new ContinuationScope("generator");
this.source = new Source();
this.continuation = new Continuation(this.scope, () -> consumer.accept(source));
this.continuation.run();
}
public boolean hasNext() {
return !continuation.isDone();
}
public T next() {
return this.source.getValue();
}
}
class VirtualThread {
private static final AtomicInteger COUNTER = new AtomicInteger(1);
public static final ContinuationScope SCOPE = new ContinuationScope("virtual-threads");
private int id;
private Continuation continuation;
public VirtualThread(Runnable runnable) {
this.id = COUNTER.getAndIncrement();
this.continuation = new Continuation(SCOPE, runnable);
}
public void run() {
System.out.println("VirtualThread " + id + " is running on " + Thread.currentThread());
continuation.run();
}
}
class VirtualThreadScheduler {
public static final VirtualThreadScheduler SCHEDULER = new VirtualThreadScheduler();
public static final ScopedValue<VirtualThread> CURRENT_VIRTUAL_THREAD = ScopedValue.newInstance();
private Queue<VirtualThread> queue = new ConcurrentLinkedQueue<>();
private ExecutorService executor = Executors.newFixedThreadPool(3);
public void start() {
while (true) {
if (!queue.isEmpty()) {
var vt = queue.remove();
executor.submit(() -> ScopedValue.where(CURRENT_VIRTUAL_THREAD, vt).run(vt::run));
}
}
}
public void schedule(VirtualThread virtualThread) {
queue.add(virtualThread);
}
}
class WaitingOperation {
public static void perform(String task, int durationInSeconds) {
System.out.printf("Waiting for %s for %d seconds\n", task, durationInSeconds);
var virtualThread = VirtualThreadScheduler.CURRENT_VIRTUAL_THREAD.get();
var timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
VirtualThreadScheduler.SCHEDULER.schedule(virtualThread);
timer.cancel();
}
}, durationInSeconds * 1000L);
Continuation.yield(VirtualThread.SCOPE);
}
}