From 2b3cfdeb9cba7f1972a4477bbd24df17352b7e5b Mon Sep 17 00:00:00 2001 From: Axel Uhl Date: Fri, 13 Sep 2019 16:59:29 +0200 Subject: [PATCH] for bug 4597, added already a ForkJoinPoolTest Change-Id: Ibe2dc23b8f94452feee6c22ceb5f362ad4203ac9 --- .../com/sap/sse/test/ForkJoinPoolTest.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100755 java/com.sap.sse.test/src/com/sap/sse/test/ForkJoinPoolTest.java diff --git a/java/com.sap.sse.test/src/com/sap/sse/test/ForkJoinPoolTest.java b/java/com.sap.sse.test/src/com/sap/sse/test/ForkJoinPoolTest.java new file mode 100755 index 00000000000..366bc2976cc --- /dev/null +++ b/java/com.sap.sse.test/src/com/sap/sse/test/ForkJoinPoolTest.java @@ -0,0 +1,58 @@ +package com.sap.sse.test; + +import static org.junit.Assert.assertEquals; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.RecursiveTask; + +import org.junit.Test; + +/** + * Tests the concept of a {@link ForkJoinPool} by producing a deep hierarchy of {@link RecursiveTask}s + * that are submitted to the pool for execution, and parent tasks wait for the children to complete. + * With a regular thread pool and regular synchronous waiting for the completion of sub-tasks the + * thread pool would get depleted, and the system would deadlock. The {@link ForkJoinPool}'s promise + * is that the threads blocked by waiting for sub-tasks will steal work from the pool, leading to the + * completion of those sub-tasks. + * + * @author Axel Uhl (d043530) + * + */ +public class ForkJoinPoolTest { + private static final int MAX_LEVEL = 20; + + private static class SomeRandomWork extends RecursiveTask { + private static final long serialVersionUID = -8187414464974452503L; + private final int level; + + public SomeRandomWork(int level) { + this.level = level; + } + + @Override + protected Long compute() { + final long result; + if (level < MAX_LEVEL) { + final SomeRandomWork leftChild = new SomeRandomWork(level+1); + final SomeRandomWork rightChild = new SomeRandomWork(level+1); + invokeAll(leftChild, rightChild); + try { + result = leftChild.get() + rightChild.get(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + } else { + result = 1; + } + return result; + } + } + + @Test + public void testDeepRecursiveWait() throws InterruptedException, ExecutionException { + ForkJoinPool pool = ForkJoinPool.commonPool(); + final Long result = pool.submit(new SomeRandomWork(0)).get(); + assertEquals(1l << MAX_LEVEL, result.longValue()); + } +}