public Statement apply()

in junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentRule.java [29:66]


    public Statement apply(final Statement statement, final FrameworkMethod frameworkMethod, final Object o) {
        return new Statement() {
            public void evaluate() throws Throwable {
                Concurrency concurrency = frameworkMethod.getAnnotation(Concurrency.class);
                if (concurrency == null)
                    statement.evaluate();
                else {
                    int nThreads = Math.max(0, concurrency.value());
                    if (nThreads == 0)
                        nThreads = Runtime.getRuntime().availableProcessors();
                    ConcurrentRunnerScheduler scheduler = new ConcurrentRunnerScheduler(
                            o.getClass().getSimpleName() + "." + frameworkMethod.getName(),
                            nThreads, nThreads);
                    final CountDownLatch go = new CountDownLatch(1);
                    Runnable runnable = new Runnable() {
                        public void run() {
                            try {
                                go.await();
                                statement.evaluate();
                            } catch (InterruptedException e) {
                                Thread.currentThread().interrupt();
                            } catch (Throwable throwable) {
                                throw ConcurrentException.wrap(throwable);
                            }
                        }
                    };
                    for (int i = 0; i < nThreads; i++)
                        scheduler.schedule(runnable);
                    go.countDown();
                    try {
                        scheduler.finished();
                    } catch (ConcurrentException e) {
                        throw e.unwrap();
                    }
                }
            }
        };
    }