forked from wesleyegberto/java-new-features
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStressVirtualThread.java
More file actions
28 lines (25 loc) · 818 Bytes
/
StressVirtualThread.java
File metadata and controls
28 lines (25 loc) · 818 Bytes
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
import java.util.concurrent.Executors;
import java.util.stream.IntStream;
/**
* Run: `java --enable-preview --source 19 <FileName.java>`
*/
public class StressVirtualThread {
public static void main(String[] args) {
var executor = Executors.newThreadPerTaskExecutor(Thread.ofVirtual().factory());
IntStream.range(0, 10_000).forEach(i -> {
executor.submit(() -> {
System.out.printf("VirtualThread %d - %s%n", i, Thread.currentThread().getName());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.printf("VirtualThread %d - %s interrupted%n", i, Thread.currentThread().getName());
} finally {
System.out.printf("VirtualThread %d - %s woke up%n", i, Thread.currentThread().getName());
}
});
});
while (!executor.isTerminated()) {
;
}
}
}