7
7
import java .util .stream .IntStream ;
8
8
9
9
public class Jep425Demo {
10
+
11
+
12
+ public static void main (String [] args ) {
13
+ long startTime = System .currentTimeMillis ();
14
+ firstVirtualThread ();
15
+ System .out .printf ("VirtualThread finished, time cost %d ms\n " ,
16
+ System .currentTimeMillis () - startTime );
17
+
18
+ firstThread ();
19
+ System .out .printf ("Thread finished, time cost %d ms\n " ,
20
+ System .currentTimeMillis () - startTime );
21
+
22
+ }
23
+
24
+
25
+
10
26
public static void firstVirtualThread () {
11
27
// 创建10000个虚拟线程
12
28
try (var executor = Executors .newVirtualThreadPerTaskExecutor ()) {
@@ -19,14 +35,18 @@ public static void firstVirtualThread() {
19
35
} // try-with-resources,会隐式调用executor.close()
20
36
}
21
37
22
- public static void main (String [] args ) {
23
- long startTime = System .currentTimeMillis ();
24
- firstVirtualThread ();
25
- System .out .printf ("firstVirtualThread finished, time cost %d ms\n " ,
26
- System .currentTimeMillis () - startTime );
38
+ public static void firstThread () {
39
+ // 创建10000个线程
40
+ try (var executor = Executors .newFixedThreadPool (1000 )) {
41
+ IntStream .range (0 , 10_000 ).forEach (i -> {
42
+ executor .submit (() -> {
43
+ Thread .sleep (Duration .ofSeconds (1 ));
44
+ return i ;
45
+ });
46
+ });
47
+ } // try-with-resources,会隐式调用executor.close()
27
48
}
28
49
29
-
30
50
private static void infoCurrentThread () {
31
51
Thread thread = Thread .currentThread ();
32
52
System .out .printf ("线程名称: %s,是否虚拟线程: %s\n " ,
0 commit comments