Fork/join example using RecursiveAction implementation.
RecursiveAction is a ForkJoinTask subclasses . This task doesn't return any value.
package com.logicbig.example;
import java.math.BigInteger;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
import java.util.concurrent.TimeUnit;
public class RecursiveActionExample {
public static void main (String[] args) {
List<BigInteger> list = new ArrayList<>();
for (int i = 3; i < 20; i++) {
list.add(new BigInteger(Integer.toString(i)));
}
ForkJoinPool.commonPool().invoke(new FactorialTask(list));
}
private static class FactorialTask extends RecursiveAction {
private static int SEQUENTIAL_THRESHOLD = 5;
private List<BigInteger> integerList;
private FactorialTask (List<BigInteger> integerList) {
this.integerList = integerList;
}
@Override
protected void compute () {
if (integerList.size() <= SEQUENTIAL_THRESHOLD) {
showFactorials();
} else {
int middle = integerList.size() / 2;
List<BigInteger> newList = integerList.subList(middle, integerList.size());
integerList = integerList.subList(0, middle);
FactorialTask task = new FactorialTask(newList);
task.fork();
this.compute();
}
}
private void showFactorials () {
for (BigInteger i : integerList) {
System.out.printf("[%s] : %s! = %s, thread = %s %n",
LocalTime.now(), i,
CalcUtil.calculateFactorial(i),
Thread.currentThread().getName());
try { //sleep to simulate long running task
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
Output
[16:03:57.560] : 7! = 5040, thread = ForkJoinPool.commonPool-worker-3
[16:03:57.560] : 15! = 1307674368000, thread = ForkJoinPool.commonPool-worker-4
[16:03:57.560] : 11! = 39916800, thread = ForkJoinPool.commonPool-worker-2
[16:03:57.559] : 3! = 6, thread = ForkJoinPool.commonPool-worker-1
[16:03:58.123] : 8! = 40320, thread = ForkJoinPool.commonPool-worker-3
[16:03:58.123] : 4! = 24, thread = ForkJoinPool.commonPool-worker-1
[16:03:58.123] : 12! = 479001600, thread = ForkJoinPool.commonPool-worker-2
[16:03:58.123] : 16! = 20922789888000, thread = ForkJoinPool.commonPool-worker-4
[16:03:58.628] : 9! = 362880, thread = ForkJoinPool.commonPool-worker-3
[16:03:58.628] : 5! = 120, thread = ForkJoinPool.commonPool-worker-1
[16:03:58.628] : 17! = 355687428096000, thread = ForkJoinPool.commonPool-worker-4
[16:03:58.628] : 13! = 6227020800, thread = ForkJoinPool.commonPool-worker-2
[16:03:59.140] : 6! = 720, thread = ForkJoinPool.commonPool-worker-1
[16:03:59.140] : 10! = 3628800, thread = ForkJoinPool.commonPool-worker-3
[16:03:59.140] : 14! = 87178291200, thread = ForkJoinPool.commonPool-worker-2
[16:03:59.140] : 18! = 6402373705728000, thread = ForkJoinPool.commonPool-worker-4
[16:03:59.683] : 19! = 121645100408832000, thread = ForkJoinPool.commonPool-worker-4
package com.logicbig.example;
import java.math.BigInteger;
public class CalcUtil {
public static BigInteger calculateFactorial (BigInteger input) {
BigInteger factorial = BigInteger.ONE;
for (BigInteger i = BigInteger.ONE;
i.compareTo(input) <= 0;
i = i.add(BigInteger.ONE)) {
factorial = factorial.multiply(i);
}
return factorial;
}
}
Original Post