/
TestParallelStreamThreads.java
40 lines (30 loc) · 1.26 KB
/
TestParallelStreamThreads.java
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
package com.github.spring.example;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.IntStream;
public class TestParallelStreamThreads {
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("pool");
withPool();
System.out.println("----------");
System.out.println("no pool");
noPool();
}
private static void withPool() throws InterruptedException, ExecutionException {
System.out.println("outside " + Thread.currentThread().getName());
ForkJoinPool customThreadPool = new ForkJoinPool(4);
customThreadPool.submit(() -> {
System.out.println("middle " + Thread.currentThread().getName());
IntStream.range(1, 10).parallel().forEach(num -> {
System.out.println("inside " + Thread.currentThread().getName());
});
}).get();
customThreadPool.shutdown();
}
private static void noPool() {
System.out.println("outside " + Thread.currentThread().getName());
IntStream.range(1, 10).parallel().forEach(num -> {
System.out.println("inside " + Thread.currentThread().getName());
});
}
}