当前位置:网站首页>并发编程系列之什么是ForkJoin框架?
并发编程系列之什么是ForkJoin框架?
2022-07-01 15:40:00 【51CTO】
并发编程系列之什么是ForkJoin框架?
1、什么是ForkJoin框架
ForkJoin框架是java的JUC包里提供的,用于处理一些比较繁重的任务,会将这个大任务分为多个小任务,多个小任务处理完成后会将结果汇总给Result,体现的是一种“分而治之”的思想。第一步,拆分fork任务,将大任务分为多个小任务;第二步,归并join,会将小任务的处理结果进行归并为一个结果。
在这里插入图片描述
2、ForkJoinTask
ForkJoinTask
是ForkJoin
框架的提供的任务API,ForkJoinTask
是一个抽象类,有两个主要的实现类,RecursiveTask
和RecursiveAction
,其中RecursiveTask
和RecursiveAction
的主要区别是,RecursiveAction
没有返回值,而RecursiveTask
是有返回值的
在这里插入图片描述
3、ForkJoinPool
ForkJoinPool类是forkjoin框架的线程池实现,基于ExecutorService接口。这个线程池是jdk1.7才加入的,用于管理线程,执行forkjoin的任务。对于线程池的使用,我们使用ThreadPoolExecutor比较多,可以在idea里看一下uml类图,可以看出ForkJoinPool和ThreadPoolExecutor实现差不多的。
在这里插入图片描述
几个重要的参数:
- parallelism:并行度,并行执行线程,可用指定,也可以不指定,不指定的情况,是根据cpu核数创建可用的线程
- ForkJoinWorkerThreadFactory:创建线程的工厂实现
- UncaughtExceptionHandler :因为未知异常中断的回调处理
- asyncMode:是否异步,默认情况是false
使用时候,可以直接创建ForkJoinPool,可以不传参,不传参的情况,默认指定的线程并行数为Runtime.getRunTime().availableProcessors();
,表示根据cpu核数创建可用线程数
也是可用传参,对并行度进行指定的public ForkJoinPool(int parallelism)
, parallelism并行度,并行执行几个线程
将forkjoin任务加入到FrokJoinPool线程池有几种方式
- execute():调用其 fork 方法在多个线程之间拆分工作。
- invoke():在ForkJoinPool线程池上调用invoke方法
- submit():返回一个Future对象,Future可以进行监控,任务完成后返回结果
4、打印斐波那契数列
ForkJoin框架可以用于一些递归遍历的场景,对于斐波那契数列,你可以比较熟悉,因为在面试中有时候经常问到,斐波那契数列的特点就是最后一项的结果等于前面两项的和
package com.example.concurrent.forkjoin;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;
/**
* <pre>
* 斐波那契数列
* </pre>
* <p>
* <pre>
* @author nicky.ma
* 修改记录
* 修改后版本: 修改人: 修改日期: 2021/10/12 16:22 修改内容:
* </pre>
*/
public class Fibonacci extends RecursiveTask<Integer>{
private int n;
public Fibonacci(int n) {
this.n = n;
}
@Override
protected Integer compute() {
if (n <= 1)
return n;
Fibonacci f1 = new Fibonacci(n - 1);
f1.fork();
Fibonacci f2 = new Fibonacci(n - 2);
f2.fork();
return f1.join() + f2.join();
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
ForkJoinPool pool = new ForkJoinPool();
for (int i = 0; i< 10; i++) {
ForkJoinTask task = pool.submit(new Fibonacci(i));
System.out.println(task.get());
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
5、ForkJoin归并排序
面试题:快速实现对一个长度百万的数组的排序
难点:可以使用归并排序,多线程如何组织实现归并排序
package com.example.concurrent.forkjoin;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
/**
* <pre>
* 大数组排序
* </pre>
* <p>
* <pre>
* @author mazq
* 修改记录
* 修改后版本: 修改人: 修改日期: 2021/10/12 17:04 修改内容:
* </pre>
*/
public class ArraySortTask extends RecursiveAction{
final long[] array; final int lo, hi;
ArraySortTask(long[] array, int lo, int hi) {
this.array = array; this.lo = lo; this.hi = hi;
}
ArraySortTask(long[] array) { this(array, 0, array.length); }
@Override
protected void compute() {
if (hi - lo < THRESHOLD)
// 少于阀值,使用Arrays.sort 快排
sortSequentially(lo, hi);
else {
/* 归并排序 */
// 取中间值
int mid = (lo + hi) >>> 1;
// 拆分任务
invokeAll(new ArraySortTask(array, lo, mid),
new ArraySortTask(array, mid, hi));
// 归并结果
merge(lo, mid, hi);
}
}
// implementation details follow:
static final int THRESHOLD = 1000;
void sortSequentially(int lo, int hi) {
Arrays.sort(array, lo, hi);
}
void merge(int lo, int mid, int hi) {
long[] buf = Arrays.copyOfRange(array, lo, mid);
for (int i = 0, j = lo, k = mid; i < buf.length; j++)
array[j] = (k == hi || buf[i] < array[k]) ?
buf[i++] : array[k++];
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
int size = 10_000;
long[] array = new long[size];
Random random = new Random();
for (int i = 0; i< size; i++) {
array[i] = random.nextInt();
}
ForkJoinPool forkJoinPool = new ForkJoinPool();
ArraySortTask task = new ArraySortTask(array , 0 , size);
forkJoinPool.submit(task);
task.get();
for (long a : array) {
System.out.println(a);
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
参考资料
边栏推荐
- HR面试:最常见的面试问题和技巧性答复
- Wechat applet 03 - text is displayed from left to right, and the block elements in the line are centered
- TS报错 Don‘t use `object` as a type. The `object` type is currently hard to use
- A unifying review of deep and shallow anomaly detection
- Summary of point cloud reconstruction methods I (pcl-cgal)
- Survey of intrusion detection systems:techniques, datasets and challenges
- 摩根大通期货开户安全吗?摩根大通期货公司开户方法是什么?
- 【ROS进阶篇】第五讲 ROS中的TF坐标变换
- Qt+pcl Chapter 6 point cloud registration ICP series 3
- 6.2 normalization 6.2.6 BC normal form (BCNF) 6.2.9 normalization summary
猜你喜欢
Survey of intrusion detection systems:techniques, datasets and challenges
【STM32学习】 基于STM32 USB存储设备的w25qxx自动判断容量检测
Detailed explanation of stm32adc analog / digital conversion
[target tracking] | template update time context information (updatenet) "learning the model update for Siamese trackers"
Qt+pcl Chapter 6 point cloud registration ICP Series 2
【一天学awk】函数与自定义函数
采集数据工具推荐,以及采集数据列表详细图解流程
Skywalking 6.4 distributed link tracking usage notes
有些能力,是工作中学不来的,看看这篇超过90%同行
张驰咨询:锂电池导入六西格玛咨询降低电池容量衰减
随机推荐
重回榜首的大众,ID依然乏力
Guide de conception matérielle du microcontrôleur s32k1xx
"Qt+pcl Chapter 6" point cloud registration ICP Series 6
Qt+pcl Chapter 6 point cloud registration ICP series 4
MySQL审计插件介绍
Implementation of wechat web page subscription message
Qt+pcl Chapter 6 point cloud registration ICP Series 2
swiper 轮播图,最后一张图与第一张图无缝衔接
Flink 系例 之 TableAPI & SQL 与 Kafka 消息获取
How to realize clock signal frequency division?
Zhang Chi Consulting: lead lithium battery into six sigma consulting to reduce battery capacity attenuation
Tableapi & SQL and Kafka message acquisition of Flink example
七夕表白攻略:教你用自己的专业说情话,成功率100%,我只能帮你们到这里了啊~(程序员系列)
What are the EN ISO 20957 certification standards for common fitness equipment
【Pygame实战】你说神奇不神奇?吃豆人+切水果结合出一款你没玩过的新游戏!(附源码)
Pnas: brain and behavior changes of social anxiety patients with empathic embarrassment
SAP CRM organization Model(组织架构模型)自动决定的逻辑分析
STM32F4-TFT-SPI时序逻辑分析仪调试记录
摩根大通期货开户安全吗?摩根大通期货公司开户方法是什么?
Zhang Chi Consulting: household appliance enterprises use Six Sigma projects to reduce customers' unreasonable return cases