Skip to content

Commit 04791a0

Browse files
committed
JEP 444: Virtual Threads
1 parent 7a3efc5 commit 04791a0

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This repository contains Java examples that are designed to track and document t
88
* [Java 21](java-21) (September, 2023)
99
* [JEP 430](https://openjdk.org/jeps/430): String Templates
1010
* [JEP 431](https://openjdk.org/jeps/431): Sequenced Collections
11+
* [JEP 444](https://openjdk.org/jeps/444): Virtual Threads
1112

1213
* [Java 16](java-16/) (March, 2021)
1314
* [JEP 395](https://openjdk.java.net/jeps/395): Records
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.ibrahimatay;
2+
3+
import java.util.concurrent.Executors;
4+
import java.util.stream.IntStream;
5+
6+
/*
7+
* JEP 444: Virtual Threads
8+
* https://openjdk.org/jeps/444
9+
* */
10+
public class JEP444VirtualThreads {
11+
public static void main(String[] args) {
12+
Runnable fn = () -> {
13+
IntStream.range(0, 100_000).forEach(i-> {
14+
System.out.println(i);
15+
try {
16+
Thread.sleep(100);
17+
} catch (InterruptedException e) {
18+
throw new RuntimeException(e);
19+
}
20+
});
21+
};
22+
23+
// Platform Threads
24+
new Thread(fn).start();
25+
26+
Thread.ofPlatform().start(fn);
27+
Thread.ofPlatform().daemon().name("my-custom-thread").unstarted(fn);
28+
29+
// Virtual Threads
30+
31+
Thread.startVirtualThread(() -> {
32+
IntStream.range(0, 100_000).forEach(i-> {
33+
System.out.println(i);
34+
try {
35+
Thread.sleep(100);
36+
} catch (InterruptedException e) {
37+
throw new RuntimeException(e);
38+
}
39+
});
40+
});
41+
42+
var executorService = Executors.newVirtualThreadPerTaskExecutor();
43+
executorService.submit(() -> {
44+
IntStream.range(0, 100_000).forEach(i-> {
45+
System.out.println(i);
46+
try {
47+
Thread.sleep(100);
48+
} catch (InterruptedException e) {
49+
throw new RuntimeException(e);
50+
}
51+
});
52+
});
53+
}
54+
}

0 commit comments

Comments
 (0)