当前位置:网站首页>【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。
点击直达
边栏推荐
- Cesium3Dtilesets 使用customShader的解读以及泛光效果示例
- There is no way to predict the rise and fall of tomorrow
- How to simply realize the function of menu dragging and sorting
- [image defogging] image defogging based on dark channel and non-mean filtering with matlab code
- 【英雄哥七月集训】第 27天:图
- @The function of valid (cascade verification) and the explanation of common constraint annotations
- 2020.7.7 eth price analysis
- Why is there no unified quotation for third-party testing fees of software products?
- POC simulation attack weapon - Introduction to nucleus (I)
- Chapter 3 business function development (batch export of market activities, Apache POI)
猜你喜欢

Interpretation of cesium3dtilesets using customshader and examples of Omni effects

One month's experience of joining Huawei OD

RTSP/Onvif协议EasyNVR视频平台一键升级方案的开发设计逻辑

Flutter神操作学习之(满级攻略)
![[signal processing] weak signal detection in communication system based on the characteristics of high-order statistics with matlab code](/img/a7/8f0d929457d285adc8020c7f5d33ea.png)
[signal processing] weak signal detection in communication system based on the characteristics of high-order statistics with matlab code

Flutter God operation learning (full level introduction)
![[leetcode] 13. linked list cycle · circular linked list](/img/58/c8796bb5ed96d09325b8f2fa6a709e.png)
[leetcode] 13. linked list cycle · circular linked list

分布式事务——Senta(一)

The virtual host website cannot access the self-test method

第二季度邮件安全报告:邮件攻击暴增4倍,利用知名品牌获取信任
随机推荐
【OpenGL】GLES20.glClear
Redis aof日志持久化
[ACNOI2022]总差一步
First knowledge of C language -- operators and keywords, define, pointer
PS simple to use
【自我成长网站收集】
One month's experience of joining Huawei OD
【图像隐藏】基于DCT、DWT、LHA、LSB的数字图像信息隐藏系统含各类攻击和性能参数附matlab代码
ORACLE BASICFILE LOB字段空间回收SHRINK SPACE的疑惑
初识C语言 -- 操作符和关键字,#define,指针
Center Based 3D object detection and tracking (centerpoint) paper notes
2022.7.8 supplement of empty Luna
How to authenticate Youxuan database client
Red hat official announced the new president and CEO! Paul Cormier, a key figure in transformation, is "retiring"
Email security report in the second quarter: email attacks have soared fourfold, and well-known brands have been used to gain trust
Is it you who are not suitable for learning programming?
Opengauss Developer Day 2022 sincerely invites you to visit the "database kernel SQL Engine sub forum" of Yunhe enmo
CNN训练循环重构——超参数测试 | PyTorch系列(二十八)
What "posture" does JD cloud have to promote industrial digitalization to climb to a "new level"?
【 图像去雾】基于暗通道和非均值滤波实现图像去雾附matlab代码