Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
43 lines (30 loc) · 1.41 KB
/
Copy pathMain.java
File metadata and controls
43 lines (30 loc) · 1.41 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
package modern.challenge;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ForkJoinPool;
import java.util.logging.Logger;
public class Main {
private static final Logger logger = Logger.getLogger(Main.class.getName());
public static void main(String[] args) {
System.setProperty("java.util.logging.SimpleFormatter.format",
"[%1$tT] [%4$-7s] %5$s %n");
int noOfProcessors = Runtime.getRuntime().availableProcessors();
logger.info(() -> "Available processors: " + noOfProcessors);
ForkJoinPool forkJoinPool = ForkJoinPool.commonPool();
int initialPoolSize = forkJoinPool.getPoolSize();
int commonPoolParallelism = ForkJoinPool.getCommonPoolParallelism();
logger.info(() -> "Common Pool parallelism :" + commonPoolParallelism);
logger.info(() -> "Common Pool size before:" + initialPoolSize);
Random rnd = new Random();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 200; i++) {
list.add(1 + rnd.nextInt(10));
}
SumRecursiveTask sumRecursiveTask = new SumRecursiveTask(list);
Integer sumAll = forkJoinPool.invoke(sumRecursiveTask);
logger.info(() -> "Final sum: " + sumAll);
int afterPoolSize = forkJoinPool.getPoolSize();
logger.info(() -> "Common Pool size after :" + afterPoolSize);
}
}