测试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)リスト<文字列> data = new ArrayList <>(); @setup public void init(){//(int i = 0; i <100; i ++){data.add(uuid.randomuuid()。toString()) }} @teardown public void destory(){// destory} @benchmark public void benchstream(){data.stream()。 } @benchmark public void benchparallelstream(){data.parallelstream()。 } public static void main(string [] args)throws runnerexception {options option opt = new optionsbuilder().include( "。*" + streambenchtest.class.getsimplename() + "。*").forks(1).build();新しいランナー(opt).run(); }}パラレルストリーム线程数
runtime.getRuntime()。利用可能なProcessors()-1、这里为7
运行结果
#完全に実行します。合計時間:00:02:44ベンチマークモードCNTスコアエラーユニットSTREAMBENCHTEST.BENCHPARALLELSTREAM AVGT 20 155868805.437±1509175.840 NS/OPSTREAMBENCHTEST.BENCHSTREAM AVGT 20 1147570372.950±6138494.414 NS
测试2
将数据データ30、同时睡眠改为100
ベンチマークモードCNTスコアエラーユニットSTREAMBENCHTEST.BENCHPARALLELSTREAM AVGT 20 414230854.631±725294.455 NS/OpStreamBenchTest.BenchStream Avgt 20 3107250608.500±4805037.628 NS/OP
可以发现睡眠越长、パラレルストリーム优势越明显。
小结
パラレルストリーム在阻塞场景下优势更明显、其线程池个数默认为
runtime.getRuntime()。利用可能なProcessors() - 1、如果需修改则需设置djava.util.concurrent.forkjoinpool.common.parallelism= 8
以上就是本次讲述知识点的全部内容、感谢你对武林网的支持。