JAVA 递归线程池 ExecutorService / ForkJoinPool

测试工具启动会递归的方式进行子进程的消息获取,目前有2种常用的ExecutorService / ForkJoinPool

为了测试哪种效果较好,我们来写个测试Demo,1到5555555,每次+1,统计每种执行完后耗时和GC数据

CompletableFuture.runAsync(() -> TestAdd(iTestMax), executorPool)
.thenRun(() -> CheckRun(iTestMax));

private void CheckRun(int iTestMax) {

if (Log < iTestMax) {
  TaskRun(iTestMax);
}

}

public void TestAdd(int iTestMax) {

synchronized (this) {
if (Log < iTestMax) {
Log = Log + 1;
}
}

}

 

 

结果很明显,递归线程池使用ForkJoinPool更佳

 

 

int nCpu = Runtime.getRuntime().availableProcessors();

ExecutorService executorPool  = Executors.newFixedThreadPool(nCpu);
ForkJoinPool forkJoinPool = new ForkJoinPool(nCpu);

 

executorPool  执行性能:

[GC (Allocation Failure) 347120K->1008K(1047040K), 0.0003305 secs]
RunTime:1544ms,TestData:5555555

 

forkJoinPool  执行性能:

[GC (Allocation Failure) 346376K->1256K(1046528K), 0.0004755 secs]
RunTime:691ms,TestData:5555555

上一篇:Android UVCCamera Java层 免驱免root即插即用


下一篇:ExecutorService等待线程完成后优雅结束