Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.googlecode.junittoolbox;
 
 
 import java.util.Deque;
import static jsr166y.ForkJoinTask.inForkJoinPool;

Encapsulates the singleton jsr166y.ForkJoinPool used by ParallelRunner and ParallelSuite to execute test classes and test methods concurrently.
class ParallelScheduler implements RunnerScheduler {
    private static final ForkJoinPool FORK_JOIN_POOL = setUpForkJoinPool();
    private static ForkJoinPool setUpForkJoinPool() {
        int numThreads;
        try {
            final String configuredNumThreads = System.getProperty("maxParallelTestThreads");
            numThreads = Math.max(2, Integer.parseInt(configuredNumThreads));
        } catch (Exception ignored) {
            Runtime runtime = Runtime.getRuntime();
            numThreads = Math.max(2, runtime.availableProcessors());
        }
        final ForkJoinPool.ForkJoinWorkerThreadFactory threadFactory = new ForkJoinPool.ForkJoinWorkerThreadFactory() {
            @Override
            public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
                final ForkJoinWorkerThread thread = ..newThread(pool);
                thread.setName("JUnit-" + thread.getName());
                return thread;
            }
        };
        return new ForkJoinPool(numThreadsthreadFactorynullfalse);
    }
    private final Deque<ForkJoinTask<?>> _asyncTasks = new LinkedList<ForkJoinTask<?>>();
    private Runnable _lastScheduledChild;
    @Override
    public void schedule(Runnable childStatement) {
        if ( != null) {
            // Execute previously scheduled child asynchronously ...
            if (inForkJoinPool()) {
                .addFirst(ForkJoinTask.adapt().fork());
            } else {
            }
        }
        // Note: We don't schedule the childStatement immediately here,
        // but remember it, so that we can synchronously execute the
        // last scheduled child in the finished method() -- this way,
        // the current thread does not immediately call join() in the
        // finished() method, which might block it ...
         = childStatement;
    }
    @Override
    public void finished() {
        final MultiException me = new MultiException();
        if ( != null) {
            if (inForkJoinPool()) {
                // Execute the last scheduled child in the current thread ...
                try { .run(); } catch (Throwable t) { me.add(t); }
            } else {
                // Submit the last scheduled child to the ForkJoinPool too,
                // because all tests should run in the worker threads ...
            }
            // Make sure all asynchronously executed children are done, before we return ...
            for (ForkJoinTask<?> task : ) {
                // Note: Because we have added all tasks via addFirst into _asyncTasks,
                // task.join() is able to steal tasks from other worker threads,
                // if there are tasks, which have not been started yet ...
                // from other worker threads ...
                try { task.join(); } catch (Throwable t) { me.add(t); }
            }
            me.throwIfNotEmpty();
        }
    }
New to GrepCode? Check out our FAQ X