Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何控制ParallelSuite线程数?_Java_Testing_Junit_Concurrency - Fatal编程技术网

Java 如何控制ParallelSuite线程数?

Java 如何控制ParallelSuite线程数?,java,testing,junit,concurrency,Java,Testing,Junit,Concurrency,我有以下代码: @RunWith(ParallelSuite.class) @Suite.SuiteClasses({Test1.class, Test2.class, Test3.class, Test4.class, Test5.class, Test6.class, Test7.class}) public class ParallelRunner { } 我运行了它,发现只有3个测试并行运行

我有以下代码:

@RunWith(ParallelSuite.class)
@Suite.SuiteClasses({Test1.class,
        Test2.class,
        Test3.class,
        Test4.class,
        Test5.class,
        Test6.class,
        Test7.class})
public class ParallelRunner {
}
我运行了它,发现只有3个测试并行运行。这些测试持续了15秒以上

是操作系统调度程序问题还是junit线程池限制


如何配置线程池限制?

它由一个
ForkedThreadPool
支持,这是工作窃取

static ForkJoinPool setUpForkJoinPool() {
    int numThreads;
    try {
        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());
    }
    ForkJoinPool.ForkJoinWorkerThreadFactory threadFactory = pool -> {
        if (pool.getPoolSize() >= pool.getParallelism()) {
            return null;
        } else {
            ForkJoinWorkerThread thread = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool);
            thread.setName("JUnit-" + thread.getName());
            return thread;
        }
    };
    return new ForkJoinPool(numThreads, threadFactory, null, false);
}

可以有效地设置
maxParallelTestThreads
,否则它将返回到处理器数量的最大值,或至少两个线程。

ParallelComputer
内部,您可以找到以下方法:

private static Runner parallelize(Runner runner) {
        if (runner instanceof ParentRunner) {
            ((ParentRunner)runner).setScheduler(new RunnerScheduler() {
                private final ExecutorService fService = Executors.newCachedThreadPool();

                public void schedule(Runnable childStatement) {
                    this.fService.submit(childStatement);
                }

                public void finished() {
                    try {
                        this.fService.shutdown();
                        this.fService.awaitTermination(9223372036854775807L, TimeUnit.NANOSECONDS);
                    } catch (InterruptedException var2) {
                        var2.printStackTrace(System.err);
                    }

                }
            });
        }

        return runner;
    }
我们需要替换这个:

private final ExecutorService fService = Executors.newCachedThreadPool();
我只是复制粘贴的
ParallelComputer
类代码,并将此行替换为:

private final ExecutorService fService = Executors.newFixedThreadPool(MAX_THREAD_COUNT);

1.我可以在哪里配置它?2.它是默认的jvm forkJoin线程池吗?粘贴的代码来自
ParallelSuite
,如果要配置它,您需要通过
maxParallelTestThreads
,或者在测试中手动设置它。否则它将是maxNumberOfCores或2个更高的值。