-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoomIntro.java
More file actions
45 lines (38 loc) · 1.66 KB
/
Copy pathLoomIntro.java
File metadata and controls
45 lines (38 loc) · 1.66 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
package base;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;
public class LoomIntro {
static void blockingCall() {
try {
Thread.sleep(100); // 100ms block
} catch (InterruptedException e) {}
}
public static void main(String[] args) throws Exception {
int TASK_COUNT = 10_000;
// Test 1: Platform Threads - Old Java
System.out.println("Testing Platform Threads...");
Instant start1 = Instant.now();
try (var executor = Executors.newFixedThreadPool(200)) {
var futures = IntStream.range(0, TASK_COUNT)
.mapToObj(i -> executor.submit(LoomIntro::blockingCall))
.toList();
for (var f : futures) f.get();
}
long time1 = Duration.between(start1, Instant.now()).toMillis();
System.out.println("200 Platform Threads: " + time1 + "ms");
// Test 2: Virtual Threads - Loom
System.out.println("\nTesting Virtual Threads...");
Instant start2 = Instant.now();
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
var futures = IntStream.range(0, TASK_COUNT)
.mapToObj(i -> executor.submit(LoomIntro::blockingCall))
.toList();
for (var f : futures) f.get();
}
long time2 = Duration.between(start2, Instant.now()).toMillis();
System.out.println("10k Virtual Threads: " + time2 + "ms");
System.out.println("\nSpeed up: " + (time1 / time2) + "x faster");
}
}