Skip to content

Commit d0fe572

Browse files
authored
Merge pull request #5 from ravening/feature/functional-java-refactor
Refactor multithreading example to functional style
2 parents 1ba6c9c + e3d92da commit d0fe572

File tree

4 files changed

+15
-19
lines changed

4 files changed

+15
-19
lines changed
3.23 KB
Binary file not shown.
2.51 KB
Binary file not shown.

src/main/java/multithreading/AppleTree.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package main.java.multithreading;
22

3-
import java.util.HashMap;
43
import java.util.Map;
54
import java.util.concurrent.TimeUnit;
5+
import java.util.stream.IntStream;
66

77
public class AppleTree {
88
private final String treeLabel;
99
private final int numberOfApples;
1010

1111
public static AppleTree[] newTreeGarden(int size) {
12-
AppleTree[] appleTrees = new AppleTree[size];
13-
for (int i = 0; i < appleTrees.length; i++) {
14-
appleTrees[i] = new AppleTree("🌳#" + i);
15-
}
16-
return appleTrees;
12+
return IntStream.range(0, size)
13+
.mapToObj(i -> new AppleTree("🌳#" + i))
14+
.toArray(AppleTree[]::new);
1715
}
1816

1917
public AppleTree(String treeLabel) {
@@ -37,12 +35,11 @@ public int pickApples() {
3735
}
3836

3937
private String toLabel(String threadName) {
40-
Map<String, String> threadNameToLabel = new HashMap<>();
41-
threadNameToLabel.put("ForkJoinPool.commonPool-worker-1", "Alice");
42-
threadNameToLabel.put("ForkJoinPool.commonPool-worker-2", "Bob");
43-
threadNameToLabel.put("ForkJoinPool.commonPool-worker-3", "Carol");
44-
threadNameToLabel.put("ForkJoinPool.commonPool-worker-4", "Dan");
45-
46-
return threadNameToLabel.getOrDefault(threadName, threadName);
38+
return Map.of(
39+
"ForkJoinPool.commonPool-worker-1", "Alice",
40+
"ForkJoinPool.commonPool-worker-2", "Bob",
41+
"ForkJoinPool.commonPool-worker-3", "Carol",
42+
"ForkJoinPool.commonPool-worker-4", "Dan"
43+
).getOrDefault(threadName, threadName);
4744
}
4845
}

src/main/java/multithreading/ForkJoinExample.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package main.java.multithreading;
22

3+
import java.util.List;
34
import java.util.concurrent.Callable;
45
import java.util.concurrent.ForkJoinPool;
5-
6-
import static java.util.Arrays.asList;
6+
import java.util.stream.IntStream;
77

88
public class ForkJoinExample {
99
public static void main(String[] args) {
@@ -13,7 +13,7 @@ public static void main(String[] args) {
1313
Callable<Void> applePicker2 = createApplePicker(appleTrees, 2, 4, "Bob");
1414
Callable<Void> applePicker3 = createApplePicker(appleTrees, 4, 6, "Carol");
1515

16-
ForkJoinPool.commonPool().invokeAll(asList(applePicker1, applePicker2, applePicker3));
16+
ForkJoinPool.commonPool().invokeAll(List.of(applePicker1, applePicker2, applePicker3));
1717

1818
System.out.println();
1919
System.out.println("All fruits collected!");
@@ -22,9 +22,8 @@ public static void main(String[] args) {
2222
public static Callable<Void> createApplePicker(AppleTree[] appleTrees, int fromIndexInclusive, int toIndexExclusive,
2323
String workerName) {
2424
return () -> {
25-
for (int i = fromIndexInclusive; i < toIndexExclusive; i++) {
26-
appleTrees[i].pickApples(workerName);
27-
}
25+
IntStream.range(fromIndexInclusive, toIndexExclusive)
26+
.forEach(i -> appleTrees[i].pickApples(workerName));
2827
return null;
2928
};
3029
}

0 commit comments

Comments
 (0)