当前位置:网站首页>【stream】并行流与顺序流
【stream】并行流与顺序流
2022-07-28 01:58:00 【张艳伟_Laura】
定义
并行流就是把一个内容分成多个数据块,并用不同的线程分别处理每个数据块的流。
Java 8 中将并行进行了优化,我们可以很容易的对数据进行并行操作。Stream API 可以声明性地通过 parallel() 与sequential() 在并行流与顺序流之间进行切换。
了解 Fork/Join 框架
Fork/Join 框架:就是在必要的情况下,将一个大任务,进行拆分(fork)成若干个
小任务(拆到不可再拆时),再将一个个的小任务运算的结果进行 join 汇总.

Fork/Join 框架与传统线程池的区别
采用“工作窃取”模式(work-stealing):
当执行新的任务时它可以将其拆分分成更小的任务执行,并将小任务加到线程队列中,然后再从一个随机线程的队列中偷一个并把它放在自己的队列中。
相对于一般的线程池实现,fork/join框架的优势体现在对其中包含的任务的处理方式上.在一般的线程池中,如果一个线程正在执行的任务由于某些原因无法继续运行,那么该线程会处于等待状态.而在fork/join框架实现中,如果某个子问题由于等待另外一个子问题的完成而无法继续运行.那么处理该子问题的线程会主动寻找其他尚未运行的子问题来执行.这种方式减少了线程的等待时间,提高了性能.
Fork/Join 框架效率高的方式:尽可能的利用CPU的资源
扩展:
通过代码测试发现
如果数据在1万以内的话,for循环效率高于foreach和stream;
如果数据量在10万的时候,stream效率最高,其次是foreach,最后是for。
另外需要注意的是如果数据达到100万的话,parallelStream异步并行处理效率最高,高于foreach和for。
点击直达
边栏推荐
- One month's experience of joining Huawei OD
- [software testing] - unittest framework for automated testing
- [data processing] boxplot drawing
- Leetcode judge whether palindrome number
- JS 事件对象 offsetX/Y clientX Y PageX Y
- Day 8 of DL
- Commissioning experience of ROS
- 写英文IEEE论文的技巧
- 没法预测明天的涨跌
- First knowledge of C language -- operators and keywords, define, pointer
猜你喜欢

Using pytorch's tensorboard visual deep learning indicators | pytorch series (25)

【ELM分类】基于核极限学习机和极限学习机实现UCI数据集分类附matlab代码

Retainface use error: modulenotfounderror: no module named'rcnn.cyton.bbox'

Confusion matrix in CNN | pytorch series (XXIII)

Pytest the best testing framework

Deep residual learning for image recognition shallow reading and Implementation

【图像隐藏】基于DCT、DWT、LHA、LSB的数字图像信息隐藏系统含各类攻击和性能参数附matlab代码

Red hat official announced the new president and CEO! Paul Cormier, a key figure in transformation, is "retiring"
![[TA frost wolf \u may - hundred people plan] Figure 3.7 TP (d) r architecture of mobile terminal](/img/9c/695294c673aa559462b4afe8eb5253.png)
[TA frost wolf \u may - hundred people plan] Figure 3.7 TP (d) r architecture of mobile terminal

使用PyTorch的TensorBoard-可视化深度学习指标 | PyTorch系列(二十五)
随机推荐
JS 事件对象2 e.charcode字符码 e.keyCode键码 盒子上下左右移动
MySQL blocking monitoring script
MySQL is shown in the figure. The existing tables a and B need to be associated with a and B tables through projectcode to find idcardnum with different addresses.
使用PyTorch的TensorBoard-可视化深度学习指标 | PyTorch系列(二十五)
2022.7.8 eth price analysis
【微信小程序开发(五)】接口按照根据开发版体验版正式版智能配置
JS中的reduce()函数介绍
修改MySQL密码的四种方法(适合初学者)
Constant power wireless charging based on stm32
@The function of valid (cascade verification) and the explanation of common constraint annotations
Retainface use error: modulenotfounderror: no module named'rcnn.cyton.bbox'
Arm32进行远程调试
【信号去噪】基于卡尔曼滤波实现信号去噪附matlab代码
Opengauss source code, what ide tools are used to manage, edit and debug?
First knowledge of C language -- operators and keywords, define, pointer
First knowledge of C language -- structure, branch and loop statements
JS event object offsetx/y clientx y pagex y
GBase8s如何在有外键关系的表中删除数据
Flutter God operation learning (full level introduction)
[wechat applet development (VI)] draw the circular progress bar of the music player