-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJEP444VirtualThreads.java
More file actions
54 lines (47 loc) · 1.52 KB
/
JEP444VirtualThreads.java
File metadata and controls
54 lines (47 loc) · 1.52 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
50
51
52
53
54
package com.ibrahimatay;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;
/*
* JEP 444: Virtual Threads
* https://openjdk.org/jeps/444
* */
public class JEP444VirtualThreads {
public static void main(String[] args) {
Runnable fn = () -> {
IntStream.range(0, 100_000).forEach(i-> {
System.out.println(i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
};
// Platform Threads
new Thread(fn).start();
Thread.ofPlatform().start(fn);
Thread.ofPlatform().daemon().name("my-custom-thread").unstarted(fn);
// Virtual Threads
Thread.startVirtualThread(() -> {
IntStream.range(0, 100_000).forEach(i-> {
System.out.println(i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
});
var executorService = Executors.newVirtualThreadPerTaskExecutor();
executorService.submit(() -> {
IntStream.range(0, 100_000).forEach(i-> {
System.out.println(i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
});
}
}