Search in sources :

Example 1 with ForkJoinWorkerThread

use of java8.util.concurrent.ForkJoinWorkerThread in project streamsupport by stefan-zobel.

the class RecursiveActionTest method testWorkerGetPool.

/**
 * getPool of current thread in pool returns its pool
 */
public void testWorkerGetPool() {
    final ForkJoinPool mainPool = mainPool();
    @SuppressWarnings("serial") RecursiveAction a = new CheckedRecursiveAction() {

        protected void realCompute() {
            ForkJoinWorkerThread w = (ForkJoinWorkerThread) Thread.currentThread();
            assertSame(mainPool, w.getPool());
        }
    };
    testInvokeOnPool(mainPool, a);
}
Also used : ForkJoinWorkerThread(java8.util.concurrent.ForkJoinWorkerThread) RecursiveAction(java8.util.concurrent.RecursiveAction) ForkJoinPool(java8.util.concurrent.ForkJoinPool)

Example 2 with ForkJoinWorkerThread

use of java8.util.concurrent.ForkJoinWorkerThread in project streamsupport by stefan-zobel.

the class ForkJoinTask8Test method testPollSubmission.

// jdk9
/**
 * pollSubmission returns unexecuted submitted task, if present
 */
public void testPollSubmission() {
    final CountDownLatch done = new CountDownLatch(1);
    final ForkJoinTask<?> a = ForkJoinTask.adapt(awaiter(done));
    final ForkJoinTask<?> b = ForkJoinTask.adapt(awaiter(done));
    final ForkJoinTask<?> c = ForkJoinTask.adapt(awaiter(done));
    final ForkJoinPool p = singletonPool();
    PoolCleaner cleaner = null;
    try {
        cleaner = cleaner(p, done);
        Thread external = new Thread(new CheckedRunnable() {

            public void realRun() {
                p.execute(a);
                p.execute(b);
                p.execute(c);
            }
        });
        @SuppressWarnings("serial") RecursiveAction s = new CheckedRecursiveAction() {

            protected void realCompute() {
                external.start();
                try {
                    external.join();
                } catch (Exception ex) {
                    threadUnexpectedException(ex);
                }
                assertTrue(p.hasQueuedSubmissions());
                assertTrue(Thread.currentThread() instanceof ForkJoinWorkerThread);
                ForkJoinTask<?> r = ForkJoinTask.pollSubmission();
                assertTrue(r == a || r == b || r == c);
                assertFalse(r.isDone());
            }
        };
        p.invoke(s);
    } finally {
        if (cleaner != null) {
            cleaner.close();
        }
    }
}
Also used : ForkJoinWorkerThread(java8.util.concurrent.ForkJoinWorkerThread) RecursiveAction(java8.util.concurrent.RecursiveAction) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) ForkJoinPool(java8.util.concurrent.ForkJoinPool) ForkJoinWorkerThread(java8.util.concurrent.ForkJoinWorkerThread)

Example 3 with ForkJoinWorkerThread

use of java8.util.concurrent.ForkJoinWorkerThread in project streamsupport by stefan-zobel.

the class RecursiveActionTest method testWorkerGetPoolIndex.

/**
 * getPoolIndex of current thread in pool returns 0 <= value < poolSize
 */
public void testWorkerGetPoolIndex() {
    final ForkJoinPool mainPool = mainPool();
    @SuppressWarnings("serial") RecursiveAction a = new CheckedRecursiveAction() {

        protected void realCompute() {
            ForkJoinWorkerThread w = (ForkJoinWorkerThread) Thread.currentThread();
            assertTrue(w.getPoolIndex() >= 0);
        // pool size can shrink after assigning index, so cannot check
        // assertTrue(w.getPoolIndex() < mainPool.getPoolSize());
        }
    };
    testInvokeOnPool(mainPool, a);
}
Also used : ForkJoinWorkerThread(java8.util.concurrent.ForkJoinWorkerThread) RecursiveAction(java8.util.concurrent.RecursiveAction) ForkJoinPool(java8.util.concurrent.ForkJoinPool)

Aggregations

ForkJoinPool (java8.util.concurrent.ForkJoinPool)3 ForkJoinWorkerThread (java8.util.concurrent.ForkJoinWorkerThread)3 RecursiveAction (java8.util.concurrent.RecursiveAction)3 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1