Skip to content

Commit fdcea19

Browse files
author
Alex Ve
committed
The example code for the article "A Guide to the Fork/Join Framework"
1 parent 923f5c4 commit fdcea19

4 files changed

Lines changed: 208 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.forkjoin;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.List;
6+
import java.util.concurrent.ForkJoinTask;
7+
import java.util.concurrent.RecursiveAction;
8+
9+
public class CustomRecursiveAction extends RecursiveAction {
10+
11+
private String workLoad = "";
12+
13+
private static final int THRESHOLD = 4;
14+
15+
public CustomRecursiveAction(String workLoad) {
16+
this.workLoad = workLoad;
17+
}
18+
19+
@Override
20+
protected void compute() {
21+
22+
if(workLoad.length() > THRESHOLD) {
23+
ForkJoinTask.invokeAll(createSubtasks());
24+
} else {
25+
processing(workLoad);
26+
}
27+
}
28+
29+
private Collection<CustomRecursiveAction> createSubtasks() {
30+
31+
List<CustomRecursiveAction> subtasks =
32+
new ArrayList<>();
33+
34+
String partOne = workLoad.substring(0, workLoad.length()/2);
35+
String partTwo = workLoad.substring(workLoad.length()/2, workLoad.length());
36+
37+
subtasks.add(new CustomRecursiveAction(partOne));
38+
subtasks.add(new CustomRecursiveAction(partTwo));
39+
40+
return subtasks;
41+
}
42+
43+
private void processing(String work) {
44+
work.toUpperCase();
45+
}
46+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.forkjoin;
2+
3+
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.Collection;
7+
import java.util.List;
8+
import java.util.concurrent.ForkJoinTask;
9+
import java.util.concurrent.RecursiveTask;
10+
11+
public class CustomRecursiveTask extends RecursiveTask<Integer> {
12+
13+
private int[] arr;
14+
15+
private static final int THRESHOLD = 20;
16+
17+
public CustomRecursiveTask(int[] arr) {
18+
this.arr = arr;
19+
}
20+
21+
@Override
22+
protected Integer compute() {
23+
24+
if (arr.length > THRESHOLD) {
25+
26+
return ForkJoinTask.invokeAll(createSubtasks())
27+
.stream()
28+
.mapToInt(ForkJoinTask::join)
29+
.sum();
30+
31+
} else {
32+
return processing(arr);
33+
}
34+
}
35+
36+
private Collection<CustomRecursiveTask> createSubtasks() {
37+
List<CustomRecursiveTask> dividedTasks = new ArrayList<>();
38+
dividedTasks.add(new CustomRecursiveTask(
39+
Arrays.copyOfRange(arr, 0, arr.length / 2)));
40+
dividedTasks.add(new CustomRecursiveTask(
41+
Arrays.copyOfRange(arr, arr.length / 2, arr.length)));
42+
return dividedTasks;
43+
}
44+
45+
private Integer processing(int[] arr) {
46+
return Arrays.stream(arr)
47+
.filter(a -> a > 10 && a < 27)
48+
.map(a -> a * 10)
49+
.sum();
50+
}
51+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.forkjoin.util;
2+
3+
4+
import java.util.concurrent.ForkJoinPool;
5+
6+
public class PoolUtil {
7+
8+
public static ForkJoinPool forkJoinPool = new ForkJoinPool(2);
9+
10+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.baeldung.java8;
2+
3+
4+
5+
import com.baeldung.forkjoin.CustomRecursiveAction;
6+
import com.baeldung.forkjoin.CustomRecursiveTask;
7+
import com.baeldung.forkjoin.util.PoolUtil;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
import java.util.Random;
12+
import java.util.concurrent.ForkJoinPool;
13+
14+
import static org.junit.Assert.*;
15+
16+
public class Java8ForkJoinTest {
17+
18+
private int[] arr;
19+
private CustomRecursiveTask customRecursiveTask;
20+
21+
@Before
22+
public void init() {
23+
Random random = new Random();
24+
arr = new int[50];
25+
for (int i = 0; i < arr.length; i++) {
26+
arr[i] = random.nextInt(35);
27+
}
28+
customRecursiveTask = new CustomRecursiveTask(arr);
29+
}
30+
31+
32+
@Test
33+
public void callPoolUtil_whenExistsAndExpectedType_thenCorrect() {
34+
35+
ForkJoinPool forkJoinPool = PoolUtil.forkJoinPool;
36+
ForkJoinPool forkJoinPoolTwo = PoolUtil.forkJoinPool;
37+
38+
assertNotNull(forkJoinPool);
39+
assertEquals(2, forkJoinPool.getParallelism());
40+
assertEquals(forkJoinPool, forkJoinPoolTwo);
41+
42+
}
43+
44+
@Test
45+
public void callCommonPool_whenExistsAndExpectedType_thenCorrect() {
46+
47+
ForkJoinPool commonPool = ForkJoinPool.commonPool();
48+
ForkJoinPool commonPoolTwo = ForkJoinPool.commonPool();
49+
50+
assertNotNull(commonPool);
51+
assertEquals(commonPool, commonPoolTwo);
52+
53+
}
54+
55+
@Test
56+
public void executeRecursiveAction_whenExecuted_thenCorrect() {
57+
58+
CustomRecursiveAction myRecursiveAction = new CustomRecursiveAction("ddddffffgggghhhh");
59+
ForkJoinPool.commonPool().invoke(myRecursiveAction);
60+
61+
assertTrue(myRecursiveAction.isDone());
62+
63+
}
64+
65+
@Test
66+
public void executeRecursiveTask_whenExecuted_thenCorrect() {
67+
68+
ForkJoinPool forkJoinPool = ForkJoinPool.commonPool();
69+
70+
forkJoinPool.execute(customRecursiveTask);
71+
int result = customRecursiveTask.join();
72+
assertTrue(customRecursiveTask.isDone());
73+
74+
forkJoinPool.submit(customRecursiveTask);
75+
int resultTwo = customRecursiveTask.join();
76+
assertTrue(customRecursiveTask.isDone());
77+
78+
}
79+
80+
@Test
81+
public void executeRecursiveTaskWithFJ_whenExecuted_thenCorrect() {
82+
83+
CustomRecursiveTask customRecursiveTaskFirst = new CustomRecursiveTask(arr);
84+
CustomRecursiveTask customRecursiveTaskSecond = new CustomRecursiveTask(arr);
85+
CustomRecursiveTask customRecursiveTaskLast = new CustomRecursiveTask(arr);
86+
87+
customRecursiveTaskFirst.fork();
88+
customRecursiveTaskSecond.fork();
89+
customRecursiveTaskLast.fork();
90+
int result = 0;
91+
result += customRecursiveTaskLast.join();
92+
result += customRecursiveTaskSecond.join();
93+
result += customRecursiveTaskFirst.join();
94+
95+
assertTrue(customRecursiveTaskFirst.isDone());
96+
assertTrue(customRecursiveTaskSecond.isDone());
97+
assertTrue(customRecursiveTaskLast.isDone());
98+
assertTrue(result != 0);
99+
100+
}
101+
}

0 commit comments

Comments
 (0)