测试 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.BearChmark) List <String> data = new ArrayList <> (); @Setup public void init () {// Prepárese para (int i = 0; i <100; i ++) {data.add (uuid.randomuuid (). ToString ()); }} @Teardown public Void Destory () {// Destory} @Benchmark public void benchstream () {data.stream (). Foreach (e -> {e.getBytes (); try {thread.seper (10);} capt (interruptedException e1) {e1.printstacktrace ();}}); } @Benchmark public void benchparallelStream () {data.ParallelStream (). Foreach (e -> {e.getBytes (); try {Thread.sleep (10);} Catch (InterruptedException e1) {e1.printstacktace ();}}); } public static void main (string [] args) lanza runnerexception {option opt = new OptionSBuilder () .include (".*" + streambenchtest.class.getsimplename () + ".*") .forks (1) .Build (); nuevo corredor (opt) .run (); }} ParallelStream 线程数
默认是 runtime.getRuntime (). Disponible Processors () - 1 , 这里为 7
运行结果
# Corre completo. Tiempo total: 00: 02: 44Benchmark Mode CNT Error de puntuación unitsstreamBenchTest.BenchParallelStream AVGT 20 155868805.437 ± 1509175.840 NS/OpStreamBenchTest.BenchStream AVGT 20 1147570372.950 ± 6138494.414 NS/OP/OP/OP/OP/OP/OP/OP
测试 2
将数据 Datos 改为 30 , 同时 Dormir 改为 100
Modo de referencia Error de puntuación CNT unitsStreamBenchTest.BenchParallelStream AVGT 20 414230854.631 ± 725294.455 NS/OpStreamBenchTest.Benchstream AVGT 20 3107250608.500 ± 4805037.628 NS/OP
可以发现 Dormir 越长 越长 , paralelstream 优势越明显。
小结
ParallelStream 在阻塞场景下优势更明显 其线程池个数默认为 其线程池个数默认为
Runtime.getRuntime (). Disponible Processors () - 1 , 如果需修改则需设置 -djava.util.concurrent.forkoinpool.common.parallelism = 8
以上就是本次讲述知识点的全部内容 感谢你对武林网的支持。 感谢你对武林网的支持。