当前位置:网站首页>ForkJoin和Stream流测试
ForkJoin和Stream流测试
2022-07-01 06:09:00 【qq_52025208】
什么是ForkJoin?
ForkJoin 在 JDK 1.7 , 并行执行任务!提高效率。大数据量! 大数据:Map Reduce (把大任务拆分为小任务)
ForkJoin特点:工作窃取
ForkJoinPool内部原理-工作窃取
在 ForkJoinPool 中,线程池中每个工作线程(ForkJoinWorkerThread)都对应一个任务队列(WorkQueue),工作线程优先处理来自自身队列的任务(LIFO或FIFO顺序,参数 mode 决定),然后以FIFO的顺序随机窃取其他队列中的任务。
import java.util.concurrent.RecursiveTask;
/** * 求和计算的任务! * 3000 6000(ForkJoin) 9000(Stream并行流) * // 如何使用 forkjoin * // 1、forkjoinPool 通过它来执行 * // 2、计算任务 forkjoinPool.execute(ForkJoinTask task) * // 3. 计算类要继承 ForkJoinTask */
public class ForkJoinDemo extends RecursiveTask<Long> {
private Long start;
private Long end;
//临界值
private Long tmp = 10000L;
public ForkJoinDemo(Long start, Long end) {
this.start = start;
this.end = end;
}
// 计算方法
@Override
protected Long compute() {
if ((end-start)<tmp){
Long sum = 0L;
for (Long i = start; i <= end; i++) {
sum += i;
}
return sum;
}else {
// forkjoin 递归
long middle = (start + end) / 2; // 中间值
ForkJoinDemo task1 = new ForkJoinDemo(start, middle);
task1.fork(); // 拆分任务,把任务压入线程队列
ForkJoinDemo task2 = new ForkJoinDemo(middle+1, end);
task2.fork(); // 拆分任务,把任务压入线程队列
return task1.join() + task2.join();
}
}
}
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;
public class Main {
public static void main(String[] args) throws ExecutionException, InterruptedException {
// test1();
// test2();
test3();
}
// 普通程序员
public static void test1(){
Long sum = 0L;
long start = System.currentTimeMillis();
for (Long i = 1L; i <= 10_0000_0000; i++) {
sum += i;
}
long end = System.currentTimeMillis();
System.out.println("sum="+sum+" 时间:"+(end-start));
}
// 会使用ForkJoin
public static void test2() throws ExecutionException, InterruptedException {
long start = System.currentTimeMillis();
ForkJoinPool pool = new ForkJoinPool();
ForkJoinTask<Long> task = new ForkJoinDemo(0L,10_0000_0000L);
ForkJoinTask<Long> submit = pool.submit(task);
Long sum = submit.get();
long end = System.currentTimeMillis();
System.out.println("sum="+sum+" 时间:"+(end-start));
}
// Stream并行流 ():range (]:rangeClosed
public static void test3(){
long start = System.currentTimeMillis();
long sum = LongStream.rangeClosed(0L,10_0000_0000L).parallel().reduce(0,Long::sum);
long end = System.currentTimeMillis();
System.out.println("sum="+sum +"时间:"+(end-start));
}
}
边栏推荐
- Retention rate of SQL required questions
- 端口扫描工具对企业有什么帮助?
- FPGA - 7系列 FPGA内部结构之Clocking -01- 时钟架构概述
- Essay learning record essay multi label Global
- HDU - 1501 zipper (memory deep search)
- Smartinstantiationawarebeanpostprocessor of the extension point series determines which construction method to execute - Chapter 432
- 【自动化运维】自动化运维平台有什么用
- Recueillir des trésors dans le palais souterrain (recherche de mémoire profonde)
- LED lighting used in health lighting
- JDBC connection pool
猜你喜欢
![Pit of kotlin bit operation (bytes[i] and 0xff error)](/img/2c/de0608c29d8af558f6f8dab4eb7fd8.png)
Pit of kotlin bit operation (bytes[i] and 0xff error)

【ManageEngine卓豪】网络运维管理是什么,网络运维平台有什么用

Multi label lsml for essay learning records

【ITSM】什么是ITSM,IT部门为什么需要ITSM

【ManageEngine】如何实现网络自动化运维

Ant new village is one of the special agricultural products that make Tiantou village in Guankou Town, Xiamen become Tiantou village

69 Cesium代码datasource加载geojson

three.js小结

Make Tiantou village sweet. Is Xianjing taro or cabbage the characteristic agricultural product of Tiantou Village

OpenGL es: (5) basic concepts of OpenGL, the process of OpenGL es generating pictures on the screen, and OpenGL pipeline
随机推荐
jdbc-连接池
让厦门灌口镇田头村变“甜头”村的特色农产品之一是
Cjc8988 Low Power Stereo codec with 2 stereo headphone drivers
数据库问题,如何优化Oracle SQL查询语句更快,效率更高
Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills
C language beginner level - realize the minesweeping game
【企业数据安全】升级备份策略 保障企业数据安全
68 cesium code datasource loading czml
SOE空间分析服务器 MySQL以及PostGres的地理空间库PostGIS防注入攻击
PLA not pasted on the bed: 6 simple solutions
Flink实战--多流合并
MinIO纠错码、分布式MinIO集群搭建及启动
Infinite horizontal marble game
SystemVerilog学习-07-类的继承和包的使用
Treasure taking from underground palace (memory based deep search)
OpenGL es: (2) relationship between OpenGL es, EGL and glsl
Servlet
基于LabVIEW的计时器
excel動態圖錶
XAF Bo of dev XPO comparison