java8中parallelStream性能测试及结果分析
测试1
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations=5,time=3,timeUnit=TimeUnit.SECONDS)
@Measurement(iterations=20,time=3,timeUnit=TimeUnit.SECONDS)
@Fork(1)
@State(Scope.Benchmark)
publicclassStreamBenchTest{
Listdata=newArrayList<>();
@Setup
publicvoidinit(){
//prepare
for(inti=0;i<100;i++){
data.add(UUID.randomUUID().toString());
}
}
@TearDown
publicvoiddestory(){
//destory
}
@Benchmark
publicvoidbenchStream(){
data.stream().forEach(e->{
e.getBytes();
try{
Thread.sleep(10);
}catch(InterruptedExceptione1){
e1.printStackTrace();
}
});
}
@Benchmark
publicvoidbenchParallelStream(){
data.parallelStream().forEach(e->{
e.getBytes();
try{
Thread.sleep(10);
}catch(InterruptedExceptione1){
e1.printStackTrace();
}
});
}
publicstaticvoidmain(String[]args)throwsRunnerException{
Optionsopt=newOptionsBuilder()
.include(".*"+StreamBenchTest.class.getSimpleName()+".*")
.forks(1)
.build();
newRunner(opt).run();
}
}
parallelStream线程数
默认是Runtime.getRuntime().availableProcessors()-1,这里为7
运行结果
#Runcomplete.Totaltime:00:02:44 BenchmarkModeCntScoreErrorUnits StreamBenchTest.benchParallelStreamavgt20155868805.437±1509175.840ns/op StreamBenchTest.benchStreamavgt201147570372.950±6138494.414ns/op
测试2
将数据data改为30,同时sleep改为100
BenchmarkModeCntScoreErrorUnits StreamBenchTest.benchParallelStreamavgt20414230854.631±725294.455ns/op StreamBenchTest.benchStreamavgt203107250608.500±4805037.628ns/op
可以发现sleep越长,parallelStream优势越明显。
小结
parallelStream在阻塞场景下优势更明显,其线程池个数默认为
Runtime.getRuntime().availableProcessors()-1,如果需修改则需设置-Djava.util.concurrent.ForkJoinPool.common.parallelism=8
以上就是本次讲述知识点的全部内容,感谢你对毛票票的支持。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下