当前位置:网站首页>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));
}
}
边栏推荐
- highmap gejson数据格式转换脚本
- Oracle create user + Role
- Primary application case of Excel DuPont analyzer
- OpenGL es: (1) origin of OpenGL es (transfer)
- Movable mechanical wall clock
- FPGA - 7系列 FPGA内部结构之Clocking -01- 时钟架构概述
- Pla ne colle pas sur le lit: 6 solutions simples
- DEV XPO对比之UOW
- skywalking集成nacos动态配置
- Fragment upload and breakpoint resume
猜你喜欢

C language beginner level - realize the minesweeping game

skywalking集成nacos动态配置

Index method and random forest to realize the information of surface water body in wet season in Shandong Province

What if the data in the cloud disk is harmonious?

Diagramme dynamique Excel

Arcserver password reset (account cannot be reset)

Tidb single machine simulation deployment production environment cluster (closed pit practice, personal test is effective)
![Pit of kotlin bit operation (bytes[i] and 0xff error)](/img/2c/de0608c29d8af558f6f8dab4eb7fd8.png)
Pit of kotlin bit operation (bytes[i] and 0xff error)

Excel dynamic chart

Multi label lsml for essay learning records
随机推荐
excel動態圖錶
交换机配置软件具有的作用
阶乘约数(唯一分解定理)
DHT11 温湿度传感器
记磁盘扇区损坏导致的Mysql故障排查
解决麒麟V10上传文件乱码问题
How does MySQL store Emoji?
相同区域 多源栅格数据 各个像元行列号一致,即行数列数相同,像元大小相同
kotlin位运算的坑(bytes[i] and 0xff 报错)
数据库问题,如何优化Oracle SQL查询语句更快,效率更高
Excel dynamic chart
无限水平大理石游戏
What if the data in the cloud disk is harmonious?
Bat operation FTP upload and download command
Oracle create user + Role
SQL必会题之留存率
SystemVerilog学习-08-随机约束和线程控制
3D打印机穿线:5种简单的解决方案
FPGA - 7系列 FPGA内部结构之Clocking -02- 时钟布线资源
c# Xml帮助类