java.util.concurrent.RecursiveTask Java Examples

The following examples show how to use java.util.concurrent.RecursiveTask. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: dragonwell8_jdk   Author: alibaba   File: ThreadLessCommon.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static void realMain(String[] args) throws Throwable {
    if (debug) {
        String pp = System.getProperty(
                "java.util.concurrent.ForkJoinPool.common.parallelism");
        System.out.println(
                "java.util.concurrent.ForkJoinPool.common.parallelism:" + pp);
        String tf = System.getProperty(
                "java.util.concurrent.ForkJoinPool.common.threadFactory");
        System.out.println(
                "java.util.concurrent.ForkJoinPool.common.threadFactory:" + tf);
    }

    long from = 0, to = 50000;
    RecursiveTask<Long> task = new SumTask(from, to, Thread.currentThread());
    long sum = task.invoke();
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);

    task.fork();
    sum = task.join();
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);

    sum = ForkJoinPool.commonPool().invoke(task.fork());
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);
}
 
Example #2
Source Project: TencentKona-8   Author: Tencent   File: ThreadLessCommon.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static void realMain(String[] args) throws Throwable {
    if (debug) {
        String pp = System.getProperty(
                "java.util.concurrent.ForkJoinPool.common.parallelism");
        System.out.println(
                "java.util.concurrent.ForkJoinPool.common.parallelism:" + pp);
        String tf = System.getProperty(
                "java.util.concurrent.ForkJoinPool.common.threadFactory");
        System.out.println(
                "java.util.concurrent.ForkJoinPool.common.threadFactory:" + tf);
    }

    long from = 0, to = 50000;
    RecursiveTask<Long> task = new SumTask(from, to, Thread.currentThread());
    long sum = task.invoke();
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);

    task.fork();
    sum = task.join();
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);

    sum = ForkJoinPool.commonPool().invoke(task.fork());
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);
}
 
Example #3
Source Project: jdk8u60   Author: chenghanpeng   File: ThreadLessCommon.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static void realMain(String[] args) throws Throwable {
    if (debug) {
        String pp = System.getProperty(
                "java.util.concurrent.ForkJoinPool.common.parallelism");
        System.out.println(
                "java.util.concurrent.ForkJoinPool.common.parallelism:" + pp);
        String tf = System.getProperty(
                "java.util.concurrent.ForkJoinPool.common.threadFactory");
        System.out.println(
                "java.util.concurrent.ForkJoinPool.common.threadFactory:" + tf);
    }

    long from = 0, to = 50000;
    RecursiveTask<Long> task = new SumTask(from, to, Thread.currentThread());
    long sum = task.invoke();
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);

    task.fork();
    sum = task.join();
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);

    sum = ForkJoinPool.commonPool().invoke(task.fork());
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);
}
 
Example #4
Source Project: j2objc   Author: google   File: RecursiveTaskTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * invokeAll(tasks) with > 2 argument invokes tasks
 */
public void testInvokeAll3() {
    RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
        public Integer realCompute() {
            FibTask f = new FibTask(8);
            FibTask g = new FibTask(9);
            FibTask h = new FibTask(7);
            invokeAll(f, g, h);
            assertTrue(f.isDone());
            assertTrue(g.isDone());
            assertTrue(h.isDone());
            checkCompletedNormally(f, 21);
            checkCompletedNormally(g, 34);
            checkCompletedNormally(h, 13);
            return NoResult;
        }};
    assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
 
Example #5
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: ThreadLessCommon.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static void realMain(String[] args) throws Throwable {
    if (debug) {
        String pp = System.getProperty(
                "java.util.concurrent.ForkJoinPool.common.parallelism");
        System.out.println(
                "java.util.concurrent.ForkJoinPool.common.parallelism:" + pp);
        String tf = System.getProperty(
                "java.util.concurrent.ForkJoinPool.common.threadFactory");
        System.out.println(
                "java.util.concurrent.ForkJoinPool.common.threadFactory:" + tf);
    }

    long from = 0, to = 50000;
    RecursiveTask<Long> task = new SumTask(from, to, Thread.currentThread());
    long sum = task.invoke();
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);

    task.fork();
    sum = task.join();
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);

    sum = ForkJoinPool.commonPool().invoke(task.fork());
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);
}
 
Example #6
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ThreadLessCommon.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static void realMain(String[] args) throws Throwable {
    if (debug) {
        String pp = System.getProperty(
                "java.util.concurrent.ForkJoinPool.common.parallelism");
        System.out.println(
                "java.util.concurrent.ForkJoinPool.common.parallelism:" + pp);
        String tf = System.getProperty(
                "java.util.concurrent.ForkJoinPool.common.threadFactory");
        System.out.println(
                "java.util.concurrent.ForkJoinPool.common.threadFactory:" + tf);
    }

    long from = 0, to = 50000;
    RecursiveTask<Long> task = new SumTask(from, to, Thread.currentThread());
    long sum = task.invoke();
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);

    task.fork();
    sum = task.join();
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);

    sum = ForkJoinPool.commonPool().invoke(task.fork());
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);
}
 
Example #7
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * join of a forked task throws exception when task completes abnormally
 */
public void testAbnormalForkJoin() {
    RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
        public Integer realCompute() {
            FailingFibTask f = new FailingFibTask(8);
            assertSame(f, f.fork());
            try {
                Integer r = f.join();
                shouldThrow();
            } catch (FJException success) {
                checkCompletedAbnormally(f, success);
            }
            return NoResult;
        }};
    assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
 
Example #8
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * timed get of a forked task throws exception when task completes abnormally
 */
public void testAbnormalForkTimedGet() {
    RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
        public Integer realCompute() throws Exception {
            FailingFibTask f = new FailingFibTask(8);
            assertSame(f, f.fork());
            try {
                Integer r = f.get(5L, SECONDS);
                shouldThrow();
            } catch (ExecutionException success) {
                Throwable cause = success.getCause();
                assertTrue(cause instanceof FJException);
                checkCompletedAbnormally(f, cause);
            }
            return NoResult;
        }};
    assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
 
Example #9
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * invoke task throws exception when task cancelled
 */
public void testCancelledInvoke() {
    RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
        public Integer realCompute() {
            FibTask f = new FibTask(8);
            assertTrue(f.cancel(true));
            try {
                Integer r = f.invoke();
                shouldThrow();
            } catch (CancellationException success) {
                checkCancelled(f);
            }
            return NoResult;
        }};
    assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
 
Example #10
Source Project: j2objc   Author: google   File: RecursiveTaskTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * invokeAll(t1, t2) throw exception if any task does
 */
public void testAbnormalInvokeAll2() {
    RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
        public Integer realCompute() {
            FibTask f = new FibTask(8);
            FailingFibTask g = new FailingFibTask(9);
            try {
                invokeAll(f, g);
                shouldThrow();
            } catch (FJException success) {
                checkCompletedAbnormally(g, success);
            }
            return NoResult;
        }};
    assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
 
Example #11
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * timed get of a forked task throws exception when task cancelled
 */
public void testCancelledForkTimedGet() {
    RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
        public Integer realCompute() throws Exception {
            FibTask f = new FibTask(8);
            assertTrue(f.cancel(true));
            assertSame(f, f.fork());
            try {
                Integer r = f.get(5L, SECONDS);
                shouldThrow();
            } catch (CancellationException success) {
                checkCancelled(f);
            }
            return NoResult;
        }};
    assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
 
Example #12
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * A reinitialized abnormally completed task may be re-invoked
 */
public void testReinitializeAbnormal() {
    RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
        public Integer realCompute() {
            FailingFibTask f = new FailingFibTask(8);
            checkNotDone(f);

            for (int i = 0; i < 3; i++) {
                try {
                    f.invoke();
                    shouldThrow();
                } catch (FJException success) {
                    checkCompletedAbnormally(f, success);
                }
                f.reinitialize();
                checkNotDone(f);
            }
            return NoResult;
        }};
    assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
 
Example #13
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * invoke task throws exception after invoking completeExceptionally
 */
public void testCompleteExceptionally() {
    RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
        public Integer realCompute() {
            FibTask f = new FibTask(8);
            f.completeExceptionally(new FJException());
            try {
                Integer r = f.invoke();
                shouldThrow();
            } catch (FJException success) {
                checkCompletedAbnormally(f, success);
            }
            return NoResult;
        }};
    assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
 
Example #14
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * invokeAll(tasks) with > 2 argument invokes tasks
 */
public void testInvokeAll3() {
    RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
        public Integer realCompute() {
            FibTask f = new FibTask(8);
            FibTask g = new FibTask(9);
            FibTask h = new FibTask(7);
            invokeAll(f, g, h);
            assertTrue(f.isDone());
            assertTrue(g.isDone());
            assertTrue(h.isDone());
            checkCompletedNormally(f, 21);
            checkCompletedNormally(g, 34);
            checkCompletedNormally(h, 13);
            return NoResult;
        }};
    assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
 
Example #15
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * invokeAll(t1, t2) throw exception if any task does
 */
public void testAbnormalInvokeAll2() {
    RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
        public Integer realCompute() {
            FibTask f = new FibTask(8);
            FailingFibTask g = new FailingFibTask(9);
            try {
                invokeAll(f, g);
                shouldThrow();
            } catch (FJException success) {
                checkCompletedAbnormally(g, success);
            }
            return NoResult;
        }};
    assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
 
Example #16
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * invokeAll(tasks) with > 2 argument throws exception if any task does
 */
public void testAbnormalInvokeAll3() {
    RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
        public Integer realCompute() {
            FibTask f = new FibTask(8);
            FailingFibTask g = new FailingFibTask(9);
            FibTask h = new FibTask(7);
            try {
                invokeAll(f, g, h);
                shouldThrow();
            } catch (FJException success) {
                checkCompletedAbnormally(g, success);
            }
            return NoResult;
        }};
    assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
 
Example #17
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * invokeAll(collection) throws exception if any task does
 */
public void testAbnormalInvokeAllCollection() {
    RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
        public Integer realCompute() {
            FailingFibTask f = new FailingFibTask(8);
            FibTask g = new FibTask(9);
            FibTask h = new FibTask(7);
            HashSet set = new HashSet();
            set.add(f);
            set.add(g);
            set.add(h);
            try {
                invokeAll(set);
                shouldThrow();
            } catch (FJException success) {
                checkCompletedAbnormally(f, success);
            }
            return NoResult;
        }};
    assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
 
Example #18
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * tryUnfork returns true for most recent unexecuted task,
 * and suppresses execution
 */
public void testTryUnfork() {
    RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
        public Integer realCompute() {
            FibTask g = new FibTask(9);
            assertSame(g, g.fork());
            FibTask f = new FibTask(8);
            assertSame(f, f.fork());
            assertTrue(f.tryUnfork());
            helpQuiesce();
            checkNotDone(f);
            checkCompletedNormally(g, 34);
            return NoResult;
        }};
    assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
}
 
Example #19
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * getSurplusQueuedTaskCount returns > 0 when
 * there are more tasks than threads
 */
public void testGetSurplusQueuedTaskCount() {
    RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
        public Integer realCompute() {
            FibTask h = new FibTask(7);
            assertSame(h, h.fork());
            FibTask g = new FibTask(9);
            assertSame(g, g.fork());
            FibTask f = new FibTask(8);
            assertSame(f, f.fork());
            assertTrue(getSurplusQueuedTaskCount() > 0);
            helpQuiesce();
            assertEquals(0, getSurplusQueuedTaskCount());
            checkCompletedNormally(f, 21);
            checkCompletedNormally(g, 34);
            checkCompletedNormally(h, 13);
            return NoResult;
        }};
    assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
}
 
Example #20
Source Project: j2objc   Author: google   File: RecursiveTaskTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * pollNextLocalTask returns least recent unexecuted task without
 * executing it, in async mode
 */
public void testPollNextLocalTaskAsync() {
    RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
        public Integer realCompute() {
            FibTask g = new FibTask(9);
            assertSame(g, g.fork());
            FibTask f = new FibTask(8);
            assertSame(f, f.fork());
            assertSame(g, pollNextLocalTask());
            helpQuiesce();
            checkCompletedNormally(f, 21);
            checkNotDone(g);
            return NoResult;
        }};
    assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a));
}
 
Example #21
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * pollNextLocalTask returns most recent unexecuted task
 * without executing it
 */
public void testPollNextLocalTask() {
    RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
        public Integer realCompute() {
            FibTask g = new FibTask(9);
            assertSame(g, g.fork());
            FibTask f = new FibTask(8);
            assertSame(f, f.fork());
            assertSame(f, pollNextLocalTask());
            helpQuiesce();
            checkNotDone(f);
            checkCompletedNormally(g, 34);
            return NoResult;
        }};
    assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
}
 
Example #22
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * pollTask returns an unexecuted task without executing it
 */
public void testPollTask() {
    RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
        public Integer realCompute() {
            FibTask g = new FibTask(9);
            assertSame(g, g.fork());
            FibTask f = new FibTask(8);
            assertSame(f, f.fork());
            assertSame(f, pollTask());
            helpQuiesce();
            checkNotDone(f);
            checkCompletedNormally(g, 34);
            return NoResult;
        }};
    assertSame(NoResult, testInvokeOnPool(singletonPool(), a));
}
 
Example #23
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * peekNextLocalTask returns least recent unexecuted task in async mode
 */
public void testPeekNextLocalTaskAsync() {
    RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
        public Integer realCompute() {
            FibTask g = new FibTask(9);
            assertSame(g, g.fork());
            FibTask f = new FibTask(8);
            assertSame(f, f.fork());
            assertSame(g, peekNextLocalTask());
            assertEquals(21, (int) f.join());
            helpQuiesce();
            checkCompletedNormally(f, 21);
            checkCompletedNormally(g, 34);
            return NoResult;
        }};
    assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a));
}
 
Example #24
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * pollNextLocalTask returns least recent unexecuted task without
 * executing it, in async mode
 */
public void testPollNextLocalTaskAsync() {
    RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
        public Integer realCompute() {
            FibTask g = new FibTask(9);
            assertSame(g, g.fork());
            FibTask f = new FibTask(8);
            assertSame(f, f.fork());
            assertSame(g, pollNextLocalTask());
            helpQuiesce();
            checkCompletedNormally(f, 21);
            checkNotDone(g);
            return NoResult;
        }};
    assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a));
}
 
Example #25
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RecursiveTaskTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * pollTask returns an unexecuted task without executing it, in
 * async mode
 */
public void testPollTaskAsync() {
    RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
        public Integer realCompute() {
            FibTask g = new FibTask(9);
            assertSame(g, g.fork());
            FibTask f = new FibTask(8);
            assertSame(f, f.fork());
            assertSame(g, pollTask());
            helpQuiesce();
            checkCompletedNormally(f, 21);
            checkNotDone(g);
            return NoResult;
        }};
    assertSame(NoResult, testInvokeOnPool(asyncSingletonPool(), a));
}
 
Example #26
Source Project: jdk8u-jdk   Author: lambdalab-mirror   File: ThreadLessCommon.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static void realMain(String[] args) throws Throwable {
    if (debug) {
        String pp = System.getProperty(
                "java.util.concurrent.ForkJoinPool.common.parallelism");
        System.out.println(
                "java.util.concurrent.ForkJoinPool.common.parallelism:" + pp);
        String tf = System.getProperty(
                "java.util.concurrent.ForkJoinPool.common.threadFactory");
        System.out.println(
                "java.util.concurrent.ForkJoinPool.common.threadFactory:" + tf);
    }

    long from = 0, to = 50000;
    RecursiveTask<Long> task = new SumTask(from, to, Thread.currentThread());
    long sum = task.invoke();
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);

    task.fork();
    sum = task.join();
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);

    sum = ForkJoinPool.commonPool().invoke(task.fork());
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);
}
 
Example #27
Source Project: j2objc   Author: google   File: RecursiveTaskTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * invokeAll(tasks) with > 2 argument throws exception if any task does
 */
public void testAbnormalInvokeAll3() {
    RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
        public Integer realCompute() {
            FibTask f = new FibTask(8);
            FailingFibTask g = new FailingFibTask(9);
            FibTask h = new FibTask(7);
            try {
                invokeAll(f, g, h);
                shouldThrow();
            } catch (FJException success) {
                checkCompletedAbnormally(g, success);
            }
            return NoResult;
        }};
    assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}
 
Example #28
Source Project: openjdk-8   Author: bpupadhyaya   File: ThreadLessCommon.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static void realMain(String[] args) throws Throwable {
    if (debug) {
        String pp = System.getProperty(
                "java.util.concurrent.ForkJoinPool.common.parallelism");
        System.out.println(
                "java.util.concurrent.ForkJoinPool.common.parallelism:" + pp);
        String tf = System.getProperty(
                "java.util.concurrent.ForkJoinPool.common.threadFactory");
        System.out.println(
                "java.util.concurrent.ForkJoinPool.common.threadFactory:" + tf);
    }

    long from = 0, to = 50000;
    RecursiveTask<Long> task = new SumTask(from, to, Thread.currentThread());
    long sum = task.invoke();
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);

    task.fork();
    sum = task.join();
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);

    sum = ForkJoinPool.commonPool().invoke(task.fork());
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);
}
 
Example #29
Source Project: jdk8u_jdk   Author: JetBrains   File: ThreadLessCommon.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static void realMain(String[] args) throws Throwable {
    if (debug) {
        String pp = System.getProperty(
                "java.util.concurrent.ForkJoinPool.common.parallelism");
        System.out.println(
                "java.util.concurrent.ForkJoinPool.common.parallelism:" + pp);
        String tf = System.getProperty(
                "java.util.concurrent.ForkJoinPool.common.threadFactory");
        System.out.println(
                "java.util.concurrent.ForkJoinPool.common.threadFactory:" + tf);
    }

    long from = 0, to = 50000;
    RecursiveTask<Long> task = new SumTask(from, to, Thread.currentThread());
    long sum = task.invoke();
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);

    task.fork();
    sum = task.join();
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);

    sum = ForkJoinPool.commonPool().invoke(task.fork());
    System.out.printf("%nSum: from [%d] to [%d] = [%d]%n", from, to, sum);
}
 
Example #30
Source Project: j2objc   Author: google   File: RecursiveTaskTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * A reinitialized normally completed task may be re-invoked
 */
public void testReinitialize() {
    RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
        public Integer realCompute() {
            FibTask f = new FibTask(8);
            checkNotDone(f);

            for (int i = 0; i < 3; i++) {
                Integer r = f.invoke();
                assertEquals(21, (int) r);
                checkCompletedNormally(f, r);
                f.reinitialize();
                f.publicSetRawResult(null);
                checkNotDone(f);
            }
            return NoResult;
        }};
    assertSame(NoResult, testInvokeOnPool(mainPool(), a));
}