Search in sources :

Example 36 with ForkJoinPool

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

the class ForkJoinTask8Test method testQuietlyInvoke.

private void testQuietlyInvoke(ForkJoinPool pool) {
    @SuppressWarnings("serial") RecursiveAction a = new CheckedRecursiveAction() {

        protected void realCompute() {
            AsyncFib f = new AsyncFib(8);
            f.quietlyInvoke();
            f.checkCompletedNormally();
        }
    };
    testInvokeOnPool(pool, a);
}
Also used : RecursiveAction(java8.util.concurrent.RecursiveAction)

Example 37 with ForkJoinPool

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

the class ForkJoinTask8Test method testInForkJoinPool.

private void testInForkJoinPool(ForkJoinPool pool) {
    @SuppressWarnings("serial") RecursiveAction a = new CheckedRecursiveAction() {

        protected void realCompute() {
            assertTrue(inForkJoinPool());
        }
    };
    testInvokeOnPool(pool, a);
}
Also used : RecursiveAction(java8.util.concurrent.RecursiveAction)

Example 38 with ForkJoinPool

use of java8.util.concurrent.ForkJoinPool 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 39 with ForkJoinPool

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

the class ForkJoinPool method tryCompensate.

/**
 * Tries to decrement counts (sometimes implicitly) and possibly
 * arrange for a compensating worker in preparation for blocking:
 * If not all core workers yet exist, creates one, else if any are
 * unreleased (possibly including caller) releases one, else if
 * fewer than the minimum allowed number of workers running,
 * checks to see that they are all active, and if so creates an
 * extra worker unless over maximum limit and policy is to
 * saturate.  Most of these steps can fail due to interference, in
 * which case 0 is returned so caller will retry. A negative
 * return value indicates that the caller doesn't need to
 * re-adjust counts when later unblocked.
 *
 * @return 1: block then adjust, -1: block without adjust, 0 : retry
 */
private int tryCompensate(WorkQueue w) {
    int t, n, sp;
    long c = ctl;
    WorkQueue[] ws = workQueues;
    if ((t = (short) (c >>> TC_SHIFT)) >= 0) {
        if (ws == null || (n = ws.length) <= 0 || w == null)
            // disabled
            return 0;
        else if ((sp = (int) c) != 0) {
            // replace or release
            WorkQueue v = ws[sp & (n - 1)];
            int wp = w.phase;
            long uc = UC_MASK & ((wp < 0) ? c + RC_UNIT : c);
            int np = sp & ~UNSIGNALLED;
            if (v != null) {
                int vp = v.phase;
                Thread vt = v.owner;
                long nc = ((long) v.stackPred & SP_MASK) | uc;
                if (vp == sp && U.compareAndSwapLong(this, CTL, c, nc)) {
                    v.phase = np;
                    if (v.source < 0)
                        LockSupport.unpark(vt);
                    return (wp < 0) ? -1 : 1;
                }
            }
            return 0;
        } else if (// reduce parallelism
        (int) (c >> RC_SHIFT) - (short) (bounds & SMASK) > 0) {
            long nc = ((RC_MASK & (c - RC_UNIT)) | (~RC_MASK & c));
            return U.compareAndSwapLong(this, CTL, c, nc) ? 1 : 0;
        } else {
            // validate
            int md = mode, pc = md & SMASK, tc = pc + t, bc = 0;
            boolean unstable = false;
            for (int i = 1; i < n; i += 2) {
                WorkQueue q;
                Thread wt;
                Thread.State ts;
                if ((q = ws[i]) != null) {
                    if (q.source == 0) {
                        unstable = true;
                        break;
                    } else {
                        --tc;
                        if ((wt = q.owner) != null && ((ts = wt.getState()) == Thread.State.BLOCKED || ts == Thread.State.WAITING))
                            // worker is blocking
                            ++bc;
                    }
                }
            }
            if (unstable || tc != 0 || ctl != c)
                // inconsistent
                return 0;
            else if (t + pc >= MAX_CAP || t >= (bounds >>> SWIDTH)) {
                Predicate<? super ForkJoinPool> sat;
                if ((sat = saturate) != null && sat.test(this))
                    return -1;
                else if (bc < pc) {
                    // lagging
                    // for retry spins
                    Thread.yield();
                    return 0;
                } else
                    throw new RejectedExecutionException("Thread limit exceeded replacing blocked worker");
            }
        }
    }
    // expand pool
    long nc = ((c + TC_UNIT) & TC_MASK) | (c & ~TC_MASK);
    return U.compareAndSwapLong(this, CTL, c, nc) && createWorker() ? 1 : 0;
}
Also used : RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Predicate(java8.util.function.Predicate)

Example 40 with ForkJoinPool

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

the class Integrate method main.

/**
 * Usage: Integrate [procs=N] [reps=N] forkPolicy=serial|dynamic|fork
 */
public static void main(String[] args) {
    final int procs = intArg(args, "procs", Runtime.getRuntime().availableProcessors());
    ForkJoinPool g = new ForkJoinPool(procs);
    for (ForkPolicy policy : ForkPolicy.values()) {
        System.out.println("Integrating from " + start + " to " + end + " forkPolicy = " + policy);
        long lastTime = System.nanoTime();
        for (int reps = intArg(args, "reps", 10); reps > 0; reps--) {
            double a;
            if (policy == ForkPolicy.SERIAL)
                a = SQuad.computeArea(g, start, end);
            else if (policy == ForkPolicy.FORK)
                a = FQuad.computeArea(g, start, end);
            else
                a = DQuad.computeArea(g, start, end);
            long now = System.nanoTime();
            double s = (double) (now - lastTime) / NPS;
            lastTime = now;
            System.out.printf("Calls/sec: %12d", (long) (calls / s));
            System.out.printf(" Time: %7.3f", s);
            System.out.printf(" Area: %12.1f", a);
            System.out.println();
            if (Math.abs(1391570583552.0 - a) > errorTolerance) {
                throw new AssertionError("wrong area: " + a);
            }
        }
        System.out.println(g);
    }
    g.shutdown();
}
Also used : ForkJoinPool(java8.util.concurrent.ForkJoinPool)

Aggregations

ForkJoinPool (java8.util.concurrent.ForkJoinPool)55 ExecutorService (java.util.concurrent.ExecutorService)32 RecursiveAction (java8.util.concurrent.RecursiveAction)32 Callable (java.util.concurrent.Callable)20 ArrayList (java.util.ArrayList)15 ExecutionException (java.util.concurrent.ExecutionException)10 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)9 Future (java.util.concurrent.Future)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 ForkJoinWorkerThread (java8.util.concurrent.ForkJoinWorkerThread)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ForkJoinTask (java8.util.concurrent.ForkJoinTask)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 CancellationException (java.util.concurrent.CancellationException)1 SynchronousQueue (java.util.concurrent.SynchronousQueue)1 TimeoutException (java.util.concurrent.TimeoutException)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Predicate (java8.util.function.Predicate)1