Skip to content

Commit 15dee86

Browse files
Java Examples
1 parent d1a5183 commit 15dee86

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.howtodoinjava.concurrency.virtualThreads;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
import java.util.concurrent.TimeUnit;
8+
9+
public class ExecutorExample {
10+
11+
void main(String[] args) throws InterruptedException {
12+
13+
List<Integer> numList = Arrays.asList(1, 2, 3, 4, 5);
14+
15+
ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
16+
17+
numList.forEach(num ->
18+
executor.execute(() -> {
19+
System.out.println(STR."Square of \{num} is :: \{square(num)}");
20+
})
21+
);
22+
23+
executor.awaitTermination(2, TimeUnit.SECONDS);
24+
executor.shutdown();
25+
}
26+
27+
private Integer square(Integer num) {
28+
return num * num;
29+
}
30+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.howtodoinjava.core.streams;
2+
3+
public class TeeingCollectorExample {
4+
5+
public static void main(String[] args) {
6+
7+
}
8+
}

0 commit comments

Comments
 (0)