diff --git a/src/pp.12.01-ForkJoinArrayFilter/README.md b/src/pp.12.01-ForkJoinArrayFilter/README.md
new file mode 100644
index 0000000..46fecb2
--- /dev/null
+++ b/src/pp.12.01-ForkJoinArrayFilter/README.md
@@ -0,0 +1,7 @@
+# Laboraufgabe "in-situ Filterung als divide and conquer"
+- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/12-01-ForkJoinArrayFilter.html)
+- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/12-Solutions.html#laboraufgabe-in-situ-filterung-als-divide-and-conquer)
+
+# Materialien
+- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/12-forkjoin.html)
+- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/12-forkjoin.html)
diff --git a/src/pp.12.01-ForkJoinArrayFilter/justfile b/src/pp.12.01-ForkJoinArrayFilter/justfile
new file mode 100644
index 0000000..87ce5f0
--- /dev/null
+++ b/src/pp.12.01-ForkJoinArrayFilter/justfile
@@ -0,0 +1,19 @@
+# Justfile (https://just.systems/) for starting Maven standard targets
+
+default:
+ just exec pp.FilterTask ""
+
+exec class args: compile
+ mvn exec:java -Dexec.args="{{args}}" -Dexec.mainClass={{class}}
+# exec class args:
+# java -cp target/app.jar {{class}} {{args}}
+clean:
+ mvn clean
+compile:
+ mvn compile
+test: compile
+ mvn test
+javadoc:
+ mvn javadoc:javadoc
+package:
+ mvn package
diff --git a/src/pp.12.01-ForkJoinArrayFilter/pom.xml b/src/pp.12.01-ForkJoinArrayFilter/pom.xml
new file mode 100644
index 0000000..fe1ad37
--- /dev/null
+++ b/src/pp.12.01-ForkJoinArrayFilter/pom.xml
@@ -0,0 +1,72 @@
+
+ 4.0.0
+ pp
+ pp.12.01-ForkJoinArrayFilter
+ 1.0-SNAPSHOT
+ jar
+
+
+ pp.FilterTask
+ 10
+ edge-SNAPSHOT
+ UTF-8
+
+
+
+ org.junit.jupiter
+ junit-jupiter
+ 5.10.0
+ test
+
+
+ org.projectlombok
+ lombok
+ ${lombok.version}
+ provided
+
+
+ net.jcip
+ jcip-annotations
+ 1.0
+ provided
+
+
+
+ clean compile exec:java
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ 3.0.0
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.9.0
+
+
+
+ org.projectlombok
+ lombok
+ ${lombok.version}
+
+
+
+
+
+ org.codehaus.mojo
+ exec-maven-plugin
+ 3.1.0
+
+
+ org.apache.maven.plugins
+ maven-javadoc-plugin
+ 3.5.0
+
+ private
+ en_US
+
+
+
+
+
diff --git a/src/pp.12.01-ForkJoinArrayFilter/src/main/java/pp/FilterTask.java b/src/pp.12.01-ForkJoinArrayFilter/src/main/java/pp/FilterTask.java
new file mode 100644
index 0000000..42528df
--- /dev/null
+++ b/src/pp.12.01-ForkJoinArrayFilter/src/main/java/pp/FilterTask.java
@@ -0,0 +1,57 @@
+package pp;
+
+import java.util.ArrayList;
+import java.util.concurrent.RecursiveAction;
+import java.util.concurrent.ForkJoinPool;
+
+@SuppressWarnings("serial")
+public class FilterTask extends RecursiveAction {
+
+ private static final int ARRAY_LEN = 16;
+ private static final int SLICE_LEN = 4;
+ private static final int MAX = 10;
+ private static int instanceCounter = 0;
+
+ private final ArrayList array;
+ private final int start;
+ private final int end;
+
+ FilterTask(ArrayList array, int start, int end) {
+ this.array = array;
+ this.start = start;
+ this.end = end;
+ synchronized(FilterTask.class) {
+ FilterTask.instanceCounter++;
+ }
+ }
+
+ @Override
+ protected void compute() {
+
+ // Fallunterscheidung einführen, die prüft, ob weiter rekursiv halbiert
+ // werden muss oder ob nun der Rekursionsanker erreicht ist
+
+ // Rekursionsanker: über Array-Elemente mit einer for-Schleife iterieren
+ // und Filter-Aktion anwenden (muss noch implementiert werden)
+
+ // Array in zwei Hälften aufteilen und beide Hälften rekursiv behandeln:
+ var mid = this.start + ((this.end - this.start) / 2);
+ var left = new FilterTask(this.array, this.start, mid);
+ var right = new FilterTask(this.array, mid, this.end);
+ left.fork();
+ right.fork();
+ right.join();
+ left.join();
+ }
+
+ public static void main(String... args) {
+ // Array initialisieren
+
+ // initialen FilterTask erzeugen und
+ // FilterTask im *Common Thread Pool* starten:
+ // ForkJoinPool.commonPool().invoke(...);
+
+ // auf das Ende warten
+ // gefiltertes Array ausgeben
+ }
+}
diff --git a/src/pp.12.01-ForkJoinArrayFilter_solution/README.md b/src/pp.12.01-ForkJoinArrayFilter_solution/README.md
new file mode 100644
index 0000000..46fecb2
--- /dev/null
+++ b/src/pp.12.01-ForkJoinArrayFilter_solution/README.md
@@ -0,0 +1,7 @@
+# Laboraufgabe "in-situ Filterung als divide and conquer"
+- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/12-01-ForkJoinArrayFilter.html)
+- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/12-Solutions.html#laboraufgabe-in-situ-filterung-als-divide-and-conquer)
+
+# Materialien
+- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/12-forkjoin.html)
+- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/12-forkjoin.html)
diff --git a/src/pp.12.01-ForkJoinArrayFilter_solution/justfile b/src/pp.12.01-ForkJoinArrayFilter_solution/justfile
new file mode 100644
index 0000000..87ce5f0
--- /dev/null
+++ b/src/pp.12.01-ForkJoinArrayFilter_solution/justfile
@@ -0,0 +1,19 @@
+# Justfile (https://just.systems/) for starting Maven standard targets
+
+default:
+ just exec pp.FilterTask ""
+
+exec class args: compile
+ mvn exec:java -Dexec.args="{{args}}" -Dexec.mainClass={{class}}
+# exec class args:
+# java -cp target/app.jar {{class}} {{args}}
+clean:
+ mvn clean
+compile:
+ mvn compile
+test: compile
+ mvn test
+javadoc:
+ mvn javadoc:javadoc
+package:
+ mvn package
diff --git a/src/pp.12.01-ForkJoinArrayFilter_solution/pom.xml b/src/pp.12.01-ForkJoinArrayFilter_solution/pom.xml
new file mode 100644
index 0000000..eb21977
--- /dev/null
+++ b/src/pp.12.01-ForkJoinArrayFilter_solution/pom.xml
@@ -0,0 +1,72 @@
+
+ 4.0.0
+ pp
+ pp.12.01-ForkJoinArrayFilter_solution
+ 1.0-SNAPSHOT
+ jar
+
+
+ pp.FilterTask
+ 10
+ edge-SNAPSHOT
+ UTF-8
+
+
+
+ org.junit.jupiter
+ junit-jupiter
+ 5.10.0
+ test
+
+
+ org.projectlombok
+ lombok
+ ${lombok.version}
+ provided
+
+
+ net.jcip
+ jcip-annotations
+ 1.0
+ provided
+
+
+
+ clean compile exec:java
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ 3.0.0
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.9.0
+
+
+
+ org.projectlombok
+ lombok
+ ${lombok.version}
+
+
+
+
+
+ org.codehaus.mojo
+ exec-maven-plugin
+ 3.1.0
+
+
+ org.apache.maven.plugins
+ maven-javadoc-plugin
+ 3.5.0
+
+ private
+ en_US
+
+
+
+
+
diff --git a/src/pp.12.01-ForkJoinArrayFilter_solution/src/main/java/pp/FilterTask.java b/src/pp.12.01-ForkJoinArrayFilter_solution/src/main/java/pp/FilterTask.java
new file mode 100644
index 0000000..0e27ecf
--- /dev/null
+++ b/src/pp.12.01-ForkJoinArrayFilter_solution/src/main/java/pp/FilterTask.java
@@ -0,0 +1,62 @@
+package pp;
+
+import java.util.ArrayList;
+import java.util.concurrent.RecursiveAction;
+import java.util.concurrent.ForkJoinPool;
+
+@SuppressWarnings("serial")
+public class FilterTask extends RecursiveAction {
+
+ private static final int ARRAY_LEN = 16;
+ private static final int SLICE_LEN = 4;
+ private static final int MAX = 10;
+ private static int instanceCounter = 0;
+
+ private final ArrayList array;
+ private final int start;
+ private final int end;
+
+ FilterTask(ArrayList array, int start, int end) {
+ this.array = array;
+ this.start = start;
+ this.end = end;
+ synchronized(FilterTask.class) {
+ FilterTask.instanceCounter++;
+ }
+ }
+
+ @Override
+ protected void compute() {
+ if ((this.end - this.start) <= SLICE_LEN) {
+ for (var i = this.start; i < this.end; i++) {
+ if (this.array.get(i) > MAX) {
+ this.array.set(i, MAX);
+ }
+ }
+ } else {
+ var mid = this.start + ((this.end - this.start) / 2);
+ var left = new FilterTask(this.array, this.start, mid);
+ var right = new FilterTask(this.array, mid, this.end);
+ left.fork();
+ right.fork();
+ right.join();
+ left.join();
+ }
+ }
+
+ public static void main(String... args) {
+ var array = new ArrayList();
+ for (var i = 0; i < ARRAY_LEN; i++) {
+ array.add(i + 1);
+ }
+ var task = new FilterTask(array, 0, array.size());
+ ForkJoinPool.commonPool().invoke(task);
+ for (var number : array) {
+ System.out.print(number + " ");
+ }
+ System.out.println("\nAnzahl der verwendeten Instanzen von FilterTask: "
+ + FilterTask.instanceCounter);
+ }
+}
+
+
diff --git a/src/pp.12.02-ForkJoinArrayReduce/README.md b/src/pp.12.02-ForkJoinArrayReduce/README.md
new file mode 100644
index 0000000..2f3dfe0
--- /dev/null
+++ b/src/pp.12.02-ForkJoinArrayReduce/README.md
@@ -0,0 +1,7 @@
+# Laboraufgabe "Reduce als *divide and conquer*"
+- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/12-02-ForkJoinArrayReduce.html)
+- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/12-Solutions.html#laboraufgabe-reduce-als-divide-and-conquer)
+
+# Materialien
+- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/12-forkjoin.html)
+- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/12-forkjoin.html)
diff --git a/src/pp.12.02-ForkJoinArrayReduce/justfile b/src/pp.12.02-ForkJoinArrayReduce/justfile
new file mode 100644
index 0000000..4157e5f
--- /dev/null
+++ b/src/pp.12.02-ForkJoinArrayReduce/justfile
@@ -0,0 +1,19 @@
+# Justfile (https://just.systems/) for starting Maven standard targets
+
+default:
+ just exec pp.ReduceTask ""
+
+exec class args: compile
+ mvn exec:java -Dexec.args="{{args}}" -Dexec.mainClass={{class}}
+# exec class args:
+# java -cp target/app.jar {{class}} {{args}}
+clean:
+ mvn clean
+compile:
+ mvn compile
+test: compile
+ mvn test
+javadoc:
+ mvn javadoc:javadoc
+package:
+ mvn package
diff --git a/src/pp.12.02-ForkJoinArrayReduce/pom.xml b/src/pp.12.02-ForkJoinArrayReduce/pom.xml
new file mode 100644
index 0000000..47869f3
--- /dev/null
+++ b/src/pp.12.02-ForkJoinArrayReduce/pom.xml
@@ -0,0 +1,72 @@
+
+ 4.0.0
+ pp
+ pp.12.02-ForkJoinArrayReduce
+ 1.0-SNAPSHOT
+ jar
+
+
+ pp.ReduceTask
+ 10
+ edge-SNAPSHOT
+ UTF-8
+
+
+
+ org.junit.jupiter
+ junit-jupiter
+ 5.10.0
+ test
+
+
+ org.projectlombok
+ lombok
+ ${lombok.version}
+ provided
+
+
+ net.jcip
+ jcip-annotations
+ 1.0
+ provided
+
+
+
+ clean compile exec:java
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ 3.0.0
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.9.0
+
+
+
+ org.projectlombok
+ lombok
+ ${lombok.version}
+
+
+
+
+
+ org.codehaus.mojo
+ exec-maven-plugin
+ 3.1.0
+
+
+ org.apache.maven.plugins
+ maven-javadoc-plugin
+ 3.5.0
+
+ private
+ en_US
+
+
+
+
+
diff --git a/src/pp.12.02-ForkJoinArrayReduce/src/main/java/pp/ReduceTask.java b/src/pp.12.02-ForkJoinArrayReduce/src/main/java/pp/ReduceTask.java
new file mode 100644
index 0000000..24553dd
--- /dev/null
+++ b/src/pp.12.02-ForkJoinArrayReduce/src/main/java/pp/ReduceTask.java
@@ -0,0 +1,21 @@
+package io.dama.ffi.forkjoin;
+
+import java.util.ArrayList;
+
+@SuppressWarnings("serial")
+public class ReduceTask {
+
+ private static final int ARRAY_LEN = 16;
+ private static final int SLICE_LEN = 4;
+
+ public static void main(String... args) {
+ var array = new ArrayList();
+ for (var i = 0; i < ARRAY_LEN; i++) {
+ array.add(i + 1);
+ }
+ // initialen ReduceTask erzeugen
+ // ReduceTask im *Common Thread Pool* starten
+ // ReduceTask im *Common Thread Pool* starten
+ // auf Ende warten und Summe ausgeben
+ }
+}
diff --git a/src/pp.12.02-ForkJoinArrayReduce_solution/README.md b/src/pp.12.02-ForkJoinArrayReduce_solution/README.md
new file mode 100644
index 0000000..2f3dfe0
--- /dev/null
+++ b/src/pp.12.02-ForkJoinArrayReduce_solution/README.md
@@ -0,0 +1,7 @@
+# Laboraufgabe "Reduce als *divide and conquer*"
+- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/12-02-ForkJoinArrayReduce.html)
+- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/12-Solutions.html#laboraufgabe-reduce-als-divide-and-conquer)
+
+# Materialien
+- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/12-forkjoin.html)
+- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/12-forkjoin.html)
diff --git a/src/pp.12.02-ForkJoinArrayReduce_solution/justfile b/src/pp.12.02-ForkJoinArrayReduce_solution/justfile
new file mode 100644
index 0000000..1d8e1a6
--- /dev/null
+++ b/src/pp.12.02-ForkJoinArrayReduce_solution/justfile
@@ -0,0 +1,27 @@
+# Justfile (https://just.systems/) for starting Maven standard targets
+
+default: reduceTask
+
+reduceTask:
+ just exec pp.ReduceTask ""
+experiment:
+ just exec pp.ReduceTaskExperiment ""
+generic:
+ just exec pp.ReduceTaskGeneric ""
+threadsnumber:
+ just exec pp.ReduceTaskThreadsNumber ""
+
+exec class args: compile
+ mvn exec:java -Dexec.args="{{args}}" -Dexec.mainClass={{class}}
+# exec class args:
+# java -cp target/app.jar {{class}} {{args}}
+clean:
+ mvn clean
+compile:
+ mvn compile
+test: compile
+ mvn test
+javadoc:
+ mvn javadoc:javadoc
+package:
+ mvn package
diff --git a/src/pp.12.02-ForkJoinArrayReduce_solution/pom.xml b/src/pp.12.02-ForkJoinArrayReduce_solution/pom.xml
new file mode 100644
index 0000000..ae6c6c6
--- /dev/null
+++ b/src/pp.12.02-ForkJoinArrayReduce_solution/pom.xml
@@ -0,0 +1,72 @@
+
+ 4.0.0
+ pp
+ pp.12.02-ForkJoinArrayReduce_solution
+ 1.0-SNAPSHOT
+ jar
+
+
+ pp.ReduceTask
+ 10
+ edge-SNAPSHOT
+ UTF-8
+
+
+
+ org.junit.jupiter
+ junit-jupiter
+ 5.10.0
+ test
+
+
+ org.projectlombok
+ lombok
+ ${lombok.version}
+ provided
+
+
+ net.jcip
+ jcip-annotations
+ 1.0
+ provided
+
+
+
+ clean compile exec:java
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ 3.0.0
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.9.0
+
+
+
+ org.projectlombok
+ lombok
+ ${lombok.version}
+
+
+
+
+
+ org.codehaus.mojo
+ exec-maven-plugin
+ 3.1.0
+
+
+ org.apache.maven.plugins
+ maven-javadoc-plugin
+ 3.5.0
+
+ private
+ en_US
+
+
+
+
+
diff --git a/src/pp.12.02-ForkJoinArrayReduce_solution/src/main/java/pp/ReduceTask.java b/src/pp.12.02-ForkJoinArrayReduce_solution/src/main/java/pp/ReduceTask.java
new file mode 100644
index 0000000..789c501
--- /dev/null
+++ b/src/pp.12.02-ForkJoinArrayReduce_solution/src/main/java/pp/ReduceTask.java
@@ -0,0 +1,50 @@
+package pp;
+
+import java.util.ArrayList;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RecursiveTask;
+
+@SuppressWarnings("serial")
+public class ReduceTask extends RecursiveTask {
+
+ private static int ARRAY_LEN = 16;
+ private static int SLICE_LEN = 4;
+
+ private final ArrayList array;
+ private final int start;
+ private final int end;
+
+ ReduceTask(ArrayList array, int start, int end) {
+ this.array = array;
+ this.start = start;
+ this.end = end;
+ }
+
+ @Override
+ protected Integer compute() {
+ var sum = 0;
+ if ((this.end - this.start) <= SLICE_LEN) {
+ for (var i = this.start; i < this.end; i++) {
+ sum += this.array.get(i);
+ }
+ } else {
+ var mid = this.start + ((this.end - this.start) / 2);
+ var left = new ReduceTask(this.array, this.start, mid);
+ var right = new ReduceTask(this.array, mid, this.end);
+ left.fork();
+ right.fork();
+ sum = left.join() + right.join();
+ }
+ return sum;
+ }
+
+ public static void main(String... args) {
+ var array = new ArrayList();
+ for (var i = 0; i < ARRAY_LEN; i++) {
+ array.add(i + 1);
+ }
+ var task = new ReduceTask(array, 0, array.size());
+ var sum = ForkJoinPool.commonPool().submit(task).join();
+ System.out.println("Summe: " + sum);
+ }
+}
diff --git a/src/pp.12.02-ForkJoinArrayReduce_solution/src/main/java/pp/ReduceTaskExperiment.java b/src/pp.12.02-ForkJoinArrayReduce_solution/src/main/java/pp/ReduceTaskExperiment.java
new file mode 100644
index 0000000..6d2f966
--- /dev/null
+++ b/src/pp.12.02-ForkJoinArrayReduce_solution/src/main/java/pp/ReduceTaskExperiment.java
@@ -0,0 +1,60 @@
+package pp;
+
+import java.util.ArrayList;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RecursiveTask;
+
+@SuppressWarnings("serial")
+public class ReduceTaskExperiment extends RecursiveTask {
+
+ private static int ARRAY_LEN = 0;
+ private static int SLICE_LEN = 0;
+
+ private final ArrayList array;
+ private final int start;
+ private final int end;
+
+ ReduceTaskExperiment(ArrayList array, int start, int end) {
+ this.array = array;
+ this.start = start;
+ this.end = end;
+ }
+
+ @Override
+ protected Integer compute() {
+ var sum = 0;
+ if ((this.end - this.start) <= SLICE_LEN) {
+ for (var i = this.start; i < this.end; i++) {
+ sum += this.array.get(i);
+ }
+ } else {
+ var mid = this.start + ((this.end - this.start) / 2);
+ var left = new ReduceTaskExperiment(this.array, this.start, mid);
+ var right = new ReduceTaskExperiment(this.array, mid, this.end);
+ left.fork();
+ right.fork();
+ sum = left.join() + right.join();
+ }
+ return sum;
+ }
+
+ public static void main(String... args) {
+ for (ARRAY_LEN = 100; ARRAY_LEN < 10000000; ARRAY_LEN = ARRAY_LEN
+ * 10) {
+ for (SLICE_LEN = 4; SLICE_LEN < 128; SLICE_LEN = SLICE_LEN * 2) {
+ var array = new ArrayList();
+ for (var i = 0; i < ARRAY_LEN; i++) {
+ array.add(i + 1);
+ }
+ var now = System.currentTimeMillis();
+ var task = new ReduceTaskExperiment(array, 0, array.size());
+ var sum = ForkJoinPool.commonPool().submit(task).join();
+ System.out.printf(
+ "ARRAY_LEN: %d, SLICE_LEN: %d, Summe: %d, Zeit: %d \n",
+ ARRAY_LEN, SLICE_LEN, sum,
+ System.currentTimeMillis() - now);
+ }
+ }
+ }
+
+}
diff --git a/src/pp.12.02-ForkJoinArrayReduce_solution/src/main/java/pp/ReduceTaskGeneric.java b/src/pp.12.02-ForkJoinArrayReduce_solution/src/main/java/pp/ReduceTaskGeneric.java
new file mode 100644
index 0000000..e02a48a
--- /dev/null
+++ b/src/pp.12.02-ForkJoinArrayReduce_solution/src/main/java/pp/ReduceTaskGeneric.java
@@ -0,0 +1,86 @@
+package pp;
+
+import java.util.ArrayList;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RecursiveTask;
+import java.util.function.BiFunction;
+
+@SuppressWarnings("serial")
+public class ReduceTaskGeneric extends RecursiveTask {
+ // Der Typ der Elemente im Container ist T (statt Integer).
+ // Dieser Typ wird Parameter von ReduceTaskGeneric.
+
+ private static int ARRAY_LEN = 16;
+ private static int SLICE_LEN = 4;
+
+ private final ArrayList array;
+ private final int start;
+ private final int end;
+
+ // generische Lösung: statt + wird die BiFunction aggregateFunc benutzt
+ // aggregateFunc ist ein Objekt, das die Funktion
+ // T apply(T x, T y)
+ // beinhaltet. Sie wird später statt + aufgerufen
+ private final BiFunction aggregFun;
+ // als Start wird ein Ersatz für 0 benötigt. Dieser initiale Wert muss vom
+ // Typ T
+ // sein
+ private final T initAcc;
+
+ ReduceTaskGeneric(BiFunction aggregFun, T initAcc,
+ ArrayList array, int start, int end) {
+ this.array = array;
+ this.start = start;
+ this.end = end;
+ // generische Lösung: aggregFun und initAcc müssen über den Konstruktor
+ // übergeben werden
+ this.aggregFun = aggregFun;
+ this.initAcc = initAcc;
+ }
+
+ @Override
+ protected T compute() { // satt Integer T als Rückgabetyp
+ var accumulator = this.initAcc; // statt 0 initAcc
+ if ((this.end - this.start) <= SLICE_LEN) {
+ for (var i = this.start; i < this.end; i++) {
+ // statt + aggregFun.apply
+ accumulator = this.aggregFun.apply(accumulator,
+ this.array.get(i));
+ }
+ } else {
+ var mid = this.start + ((this.end - this.start) / 2);
+ var left = new ReduceTaskGeneric<>(this.aggregFun, this.initAcc,
+ this.array, this.start, mid);
+ var right = new ReduceTaskGeneric<>(this.aggregFun, this.initAcc,
+ this.array, mid, this.end);
+ left.fork();
+ right.fork();
+
+ // statt + aggregFun.apply
+ accumulator = this.aggregFun.apply(left.join(), right.join());
+ }
+ return accumulator;
+ }
+
+ public static void main(String... args) {
+ var array = new ArrayList();
+ for (var i = 0; i < ARRAY_LEN; i++) {
+ array.add(i + 1);
+ }
+ // Hier wird die aggregFun als Lambda-Ausdruck übergeben: (a, b) -> a +
+ // b
+ // Der Typ-Parameter von ReduceTaskGeneric wird dementsprechend auf
+ // Integer
+ // festgelegt.
+ // Der initiale Wert des +-Akkumulators ist 0
+ var task = new ReduceTaskGeneric<>(//
+ ((a, b) -> a + b), // Hier wird die aggregFun als
+ // Lambda-Ausdruck übergeben: +
+ 0, // Der initiale Wert des +-Akkumulators ist 0.
+ array, 0, array.size());
+ ForkJoinPool.commonPool().invoke(task);
+ // seit Java 8 alternativ: task.invoke();
+ var sum = task.join();
+ System.out.println("Summe: " + sum);
+ }
+}
diff --git a/src/pp.12.02-ForkJoinArrayReduce_solution/src/main/java/pp/ReduceTaskThreadsNumber.java b/src/pp.12.02-ForkJoinArrayReduce_solution/src/main/java/pp/ReduceTaskThreadsNumber.java
new file mode 100644
index 0000000..622a6c8
--- /dev/null
+++ b/src/pp.12.02-ForkJoinArrayReduce_solution/src/main/java/pp/ReduceTaskThreadsNumber.java
@@ -0,0 +1,56 @@
+package pp;
+
+import java.util.ArrayList;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RecursiveTask;
+
+@SuppressWarnings("serial")
+public class ReduceTaskThreadsNumber extends RecursiveTask {
+
+ private static int ARRAY_LEN = 280;
+ private static int SLICE_LEN = ARRAY_LEN / ForkJoinPool.commonPool().getParallelism();
+ private static int instanceCounter = 0;
+
+ private final ArrayList array;
+ private final int start;
+ private final int end;
+
+ ReduceTaskThreadsNumber(ArrayList array, int start, int end) {
+ this.array = array;
+ this.start = start;
+ this.end = end;
+ instanceCounter++;
+ }
+
+ @Override
+ protected Integer compute() {
+ var sum = 0;
+ if ((this.end - this.start) <= SLICE_LEN) {
+ for (var i = this.start; i < this.end; i++) {
+ sum += this.array.get(i);
+ }
+ } else {
+ var mid = this.start + ((this.end - this.start) / 2);
+ var left = new ReduceTaskThreadsNumber(this.array, this.start, mid);
+ var right = new ReduceTaskThreadsNumber(this.array, mid, this.end);
+ left.fork();
+ right.fork();
+ sum = left.join() + right.join();
+ }
+ return sum;
+ }
+
+ public static void main(String... args) {
+ var array = new ArrayList();
+ for (var i = 0; i < ARRAY_LEN; i++) {
+ array.add(i + 1);
+ }
+ var task = new ReduceTaskThreadsNumber(array, 0, array.size());
+ var sum = ForkJoinPool.commonPool().submit(task).join();
+ System.out.println("Summe: " + sum);
+ System.out.println("instanceCounter: " + instanceCounter);
+ System.out.println(
+ "Parallelism: " + ForkJoinPool.commonPool().getParallelism());
+ System.out.println("ARRAY_LEN: " + ARRAY_LEN);
+ }
+}