sequential / parallel
sequential 返回的流是串行处理(默认就是,可以不写)
parallel返回的流是并行处理,参考如下测试用例:
@Test
public void test12() throws Exception {
IntStream intStream = IntStream.of(6, 1, 1, 2, 5, 2, 3, 4);
long start=System.currentTimeMillis();
//并行处理
intStream.parallel().forEach(x->{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+":"+x);
});
System.out.println("parallel time->"+(System.currentTimeMillis()-start));
intStream = IntStream.of(6, 1, 1, 2, 5, 2, 3, 4);
start=System.currentTimeMillis();
//默认都是串行处理
intStream.sequential().forEach(x->{
try {
Thread.sleep(1000);