-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVThreadDemo.java
More file actions
35 lines (29 loc) · 1.07 KB
/
Copy pathVThreadDemo.java
File metadata and controls
35 lines (29 loc) · 1.07 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
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
public class VThreadDemo {
public static void main(String[] args) throws InterruptedException {
var max = 100L;
final List<Thread> threads = new ArrayList<>();
final Instant startTime = Instant.now();
for(var i = 0; i < max; i++){
final var tid = i;
final Thread t = Thread.startVirtualThread(() -> {
try {
System.out.println("Hi from "+tid+" in "+Thread.currentThread());
Thread.sleep(1000);
System.out.println("Bye from "+tid+" in "+Thread.currentThread());
} catch (InterruptedException e) {
e.printStackTrace();
}
});
threads.add(t);
}
for (Thread t : threads) {
t.join();
}
final Instant endTime = Instant.now();
System.out.println("Time elapsed "+ Duration.between(startTime, endTime).toString());
}
}