测试 1
@Benchmarkmode (mode.averageTime) @OutputTimeUnit (timeUnit.nanosEconds) @warmup (iterations = 5, time = 3, timeUnit = timeUnit.SeConds) @Measucuring (iterações = 20, tempo = 3, timeUnit = timeUnit.SeConds) @Fork (1) dados = novo ArrayList <> (); @Setup public void init () {// Prepare -se 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.sleep (10);} catch (interruptException}} {e1.prstrace; } @Benchmark public void benchparalLelsTream () {data.paralLelsTream (). Foreach (e -> {e.getBytes (); tente {thread.sleep (10);} catch (interruptedException e1) {e1.printstacktrace ();}}); } public static void main (string [] args) lança runnerexception {options opt = new optionsbuilder () .include (".*" + streambenchtest.class.getsimplename () + ".*") .forks (1) .build (); novo corredor (opt) .run (); }} ParallelStream 线程数
默认是 Runtime.getRuntime (). DisponívelProcessors () - 1 , 这里为 7
运行结果
# Execute completo. Tempo total: 00: 02: 44 BENCENDA MODO DE CNT CNT
测试 2
将数据 Dados 改为 30 , Sleep 改为 100
Modo de referência CNT ERRO DE ERRO UNITSTREAMBENCHTEST.BenchParalLelStream Avgt 20 414230854.631 ± 725294.455 NS/OPSTREAMBENCHTEST.BenchStream Avgt 20 31072506060.500 ± 48050377.628 NS/Op
可以发现 Sono 越长 , ParallelStream 优势越明显。
小结
ParallelStream 在阻塞场景下优势更明显 , 其线程池个数默认为
RUNTIME.GETRUNTIME (). DisponívelProcessors () - 1 , 如果需修改则需设置 -djava.util.concurrent.forkjoinpool.common.paralelismo = 8
以上就是本次讲述知识点的全部内容 , 感谢你对武林网的支持。