定义一个测试类

public class TestParallelStream {

    private List<Integer> list;
private int size;
private CountDownLatch countDownLatch;
@Before
public void initList(){
list = new ArrayList<>();
size = 100;
countDownLatch = new CountDownLatch(size);
for(int i=0; i<size; i++) {
list.add(i);
}
}

上面定义了一个100元素的list。

下面使用迭代器遍历:

/**
* 迭代器遍历
* @throws Exception
*/
@Test
public void testFor() throws Exception {
long start = System.currentTimeMillis();
Iterator<Integer> iterator = list.iterator();
while(iterator.hasNext()){
System.out.print(iterator.next());
}
System.out.println(); long end = System.currentTimeMillis(); System.out.println(end-start);
}

结果耗时稳定一位数的毫秒

使用parallelStream的方式:

/**
* 使用parallelSteam.forEach()遍历
* @throws Exception
*/
@Test
public void testListForEach() throws Exception{
long start = System.currentTimeMillis();
list.parallelStream().forEach(
l -> {
System.out.print(l); countDownLatch.countDown();
}
);
countDownLatch.await();
System.out.println(); long end = System.currentTimeMillis(); System.out.println(end-start);
}

结果是稳定在50以上的两位数的毫秒。

但是当我们要进行耗时的操作时,比如说IO,这里用Thread.sleep(100)模拟IO。

用迭代器处理模拟的IO的方式:

/**
* 当有耗时操作时,使用迭代器遍历
* @throws Exception
*/
@Test
public void testForSleep() throws Exception {
long start = System.currentTimeMillis();
Iterator<Integer> iterator = list.iterator();
while(iterator.hasNext()){
System.out.print(iterator.next());
Thread.sleep();
}
System.out.println(); long end = System.currentTimeMillis(); System.out.println(end-start);
}

结果是比大一些的毫秒数。

用parallelStream处理模拟的IO:

/**
* 当有耗时操作时,使用parallelSteam.forEach()遍历
* @throws Exception
*/
@Test
public void testListParallelStream() throws Exception{
long start = System.currentTimeMillis();
list.parallelStream().forEach(
l -> {
System.out.print(l);
try {
Thread.sleep();
} catch (InterruptedException e) {
e.printStackTrace();
}
countDownLatch.countDown();
}
);
countDownLatch.await();
System.out.println(); long end = System.currentTimeMillis(); System.out.println(end-start);
}

结果是比大一些的毫秒数。应该是跟我电脑4核有关,处理4个线程。是上面迭代器遍历时间的1/4.

总结

当数据量不大或者没有太耗时的操作时,顺序执行(如iterator)往往比并行执行更快,毕竟,分配资源、准备线程池和其它相关资源也是需要时间的;

当任务涉及到耗时操作(如I/O)并且任务之间不互相依赖时,那么并行化就是一个不错的选择。通常而言,将这类程序并行化之后,执行速度会提升好几个等级;

由于在并行环境中任务的执行顺序是不确定的,因此对于依赖于顺序的任务而言,并行化也许不能给出正确的结果。

参考:

https://blog.csdn.net/u011001723/article/details/52794455/

Java8 parallelStream与迭代器Iterator性能的更多相关文章

  1. Python进阶内容(四)--- 迭代器(Iterator)与生成器(Generator)

    迭代器 我们已经知道,可以直接作用于for循环的数据类型有以下几种: 一类是集合数据类型,如list.tuple.dict.set.str等: 一类是generator,包括生成器和带yield的ge ...

  2. 牛客网Java刷题知识点之Java 集合框架的构成、集合框架中的迭代器Iterator、集合框架中的集合接口Collection(List和Set)、集合框架中的Map集合

    不多说,直接上干货! 集合框架中包含了大量集合接口.这些接口的实现类和操作它们的算法. 集合容器因为内部的数据结构不同,有多种具体容器. 不断的向上抽取,就形成了集合框架. Map是一次添加一对元素. ...

  3. 用struts2标签如何从数据库获取数据并在查询页面显示。最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变量。

    最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变 ...

  4. VC++ 迭代器 iterator, const_iterator, const iterator

    迭代器 iterator, const_iterator, const iterator 迭代器iterator的作用类似于指针. (1)iterator只有针对制定<类型>的容器才有效. ...

  5. 迭代器iterator

    现在接着上篇的,写一来标识vector 的元素的对象迭代器iterator: 还是通过具体代码举例: 下面我讲了一个我暑期团队的故事哦~~: #include<iostream> #inc ...

  6. c++中的迭代器 iterator

    迭代器iterator的作用类似于指针. iterator只有针对制定<类型>的容器才有效.例如: vector<int> vec; vector<int>::it ...

  7. Python的迭代器(iterator)和生成器(constructor)

    一.迭代器(iterator) 1.迭代器的概述 在Python中,for循环可以用于Python中的任何类型,包括列表.元祖等等,实际上,for循环可用于任何“可迭代对象”,这其实就是迭代器 迭代器 ...

  8. 设计模式 - 迭代模式(iterator pattern) Java 迭代器(Iterator) 详细解释

    迭代模式(iterator pattern) Java 迭代器(Iterator) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 參考迭代器模式(ite ...

  9. Java 实现迭代器(Iterator)模式

    类图 /** * 自己定义集合接口, 相似java.util.Collection * 用于数据存储 * @author stone * */ public interface ICollection ...

随机推荐

  1. linux中tree命令

    需要安装tree包(安装:yum -y install tree). tree命令的选项说明如下: [ 匹配选项:] -L:用于指定递归显示的深度,指定的深度必须是大于0的整数. -P:用于显示统配符 ...

  2. 【Selenium-WebDriver自学】Selenium-IDE调试(四)

    ==================================================================================================== ...

  3. docker unbuntu 32-bit 更新apt-get

    1.vi /etc/apt/sources.list deb http://mirrors.aliyun.com/debian wheezy main contrib non-free deb-src ...

  4. python变量存储和深浅拷贝

    python的变量及其存储 在高级语言中,变量是对内存及其地址的抽象.对于python而言,python的一切变量都是对象,变量的存储,采用了引用语义的方式,存储的只是一个变量的值所在的内存地址,而不 ...

  5. ROS进阶学习笔记(11)- Turtlebot Navigation and SLAM - ROSMapModify - ROS地图修改

    ROS进阶学习笔记(11)- Turtlebot Navigation and SLAM - 2 - MapModify地图修改 We can use gmapping model to genera ...

  6. SimpleDateFormat线程不安全

    http://www.cnblogs.com/peida/archive/2013/05/31/3070790.html dateUtil替换

  7. 3.Appnium的安装

    Appnium:移动端的自动测试话工具,类似selenium,可利用其驱动手机端去模拟点击.滑动.输入操作. 下载地址:https://github.com/appium/appium-desktop ...

  8. 我练就数据分析技能从HR转型为产品经理

    本文转自知乎 作者:空白白白白 ----------------------------------------------------- 空白白白白写在前面:当我在奥兰多的时候,一位漂亮的女学员(看 ...

  9. JavaScript 函数与对象的 简单区别

    直接上例子 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <met ...

  10. spring 的 切片Aspect 最常用记录方法执行时间

    /** * */ package com.icil.esolution.aspect; import java.util.Date; import org.aspectj.lang.Proceedin ...