本文主要是介绍ForkJoin的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
可以先简单了解下【JDK中的ForkJoin机制】。
1、使用ForkJoin来实现数组中元素排序,使用invoke方法和submit方法。
package com.su.mybatis.oracle.controller;import java.util.Arrays;
import java.util.Random;
//import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
//import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;import com.alibaba.fastjson.JSON;public class Test {public static void main(String[] args) {long startTime = System.currentTimeMillis();ForkJoinPool pool = new ForkJoinPool();int[] array = produceArray(100);System.out.println("排序前:" + JSON.toJSONString(array));SortTask task = new SortTask(array);//1、使用invoke方法(同步)int[] result = pool.invoke(task);System.out.println("排序后:" + JSON.toJSONString(result));
// //2、使用submit方法(异步)
// ForkJoinTask<int[]> result = pool.submit(task);
// try {
// System.out.println("排序后:" + JSON.toJSONString(result.get()));
// } catch (InterruptedException e) {
// e.printStackTrace();
// } catch (ExecutionException e) {
// e.printStackTrace();
// }System.out.println("花费时间:" + (System.currentTimeMillis()- startTime) + "ms");}static class SortTask extends RecursiveTask<int[]> {private static final long serialVersionUID = -2039880513818337443L;private final int LIMIT = 2;private int[] array;public SortTask(int[] array) {this.array = array;}@Overrideprotected int[] compute() {if(array.length <= LIMIT){//插入排序return insertionSort(array);}else{//归并排序int mid = array.length / 2;SortTask leftTask = new SortTask(Arrays.copyOfRange(array, 0, mid));SortTask rightTask = new SortTask(Arrays.copyOfRange(array, mid, array.length));invokeAll(leftTask,rightTask);int[] leftResult = leftTask.join();int[] rightResult = rightTask.join();return merge(leftResult,rightResult);}}}//生成随机数组public static int[] produceArray(int arrayLength) {Random r = new Random();int[] array = new int[arrayLength];for(int i=0;i<arrayLength;i++){array[i] = r.nextInt(arrayLength*arrayLength);}return array;}/** 两个数组合并成一个数组,升序排列 */public static int[] merge(int[] left, int[] right) {int[] result = new int[left.length + right.length];for (int index = 0, i = 0, j = 0; index < result.length; index++) {if (i >= left.length)result[index] = right[j++];else if (j >= right.length)result[index] = left[i++];else if (left[i] > right[j])result[index] = right[j++];elseresult[index] = left[i++];}return result;}/** 插入排序 */public static int[] insertionSort(int[] array) {if (array.length == 0)return array;int currentValue;for (int i = 0; i < array.length - 1; i++) {int preIndex = i;currentValue = array[preIndex + 1];while (preIndex >= 0 && currentValue < array[preIndex]) {array[preIndex + 1] = array[preIndex];preIndex--;}array[preIndex + 1] = currentValue;}return array;}
}
使用invoke方法,输出结果:
排序前:[4310,1357,7626,506,1766,1430,8181,4019,6694,4816,445,962,6648,5475,7002,6318,2765,6712,6553,3651,2939,4180,6427,4941,2380,846,265,6194,661,769,5090,687,7633,2780,868,8475,5706,1751,1778,280,4356,6881,4040,4391,9366,6116,1080,3035,533,6938,495,9190,8879,5353,858,4624,4081,3945,5119,7223,966,9319,4569,5328,9420,7010,6899,3034,9636,2578,4480,9699,5494,4456,7893,7989,2463,3969,9204,5656,1912,5972,8246,459,4740,2987,6931,7741,9429,974,6913,2150,5596,1338,183,787,9067,6945,765,925]
排序后:[183,265,280,445,459,495,506,533,661,687,765,769,787,846,858,868,925,962,966,974,1080,1338,1357,1430,1751,1766,1778,1912,2150,2380,2463,2578,2765,2780,2939,2987,3034,3035,3651,3945,3969,4019,4040,4081,4180,4310,4356,4391,4456,4480,4569,4624,4740,4816,4941,5090,5119,5328,5353,5475,5494,5596,5656,5706,5972,6116,6194,6318,6427,6553,6648,6694,6712,6881,6899,6913,6931,6938,6945,7002,7010,7223,7626,7633,7741,7893,7989,8181,8246,8475,8879,9067,9190,9204,9319,9366,9420,9429,9636,9699]
花费时间:142ms
使用submit方法,输出结果:
排序前:[4788,5561,3948,2328,6607,6488,7901,7459,8016,9959,2340,7412,7571,4884,919,5636,4525,2477,5363,7187,9488,9975,7424,1557,8829,7998,5775,5691,8884,3433,5567,8473,8696,5353,3215,5097,5326,2302,4191,2924,8595,5654,2783,5416,429,35,7385,1426,6138,56,4970,5513,917,9216,1835,4749,1623,6330,4030,8618,793,4869,468,6885,7880,2420,2491,7457,1825,3470,8453,6142,6892,8838,7636,832,8214,7041,961,3975,3014,7049,1307,5648,6769,6045,1405,7943,6744,4530,8744,4855,5135,939,3360,2416,5769,2720,2179,8052]
排序后:[35,56,429,468,793,832,917,919,939,961,1307,1405,1426,1557,1623,1825,1835,2179,2302,2328,2340,2416,2420,2477,2491,2720,2783,2924,3014,3215,3360,3433,3470,3948,3975,4030,4191,4525,4530,4749,4788,4855,4869,4884,4970,5097,5135,5326,5353,5363,5416,5513,5561,5567,5636,5648,5654,5691,5769,5775,6045,6138,6142,6330,6488,6607,6744,6769,6885,6892,7041,7049,7187,7385,7412,7424,7457,7459,7571,7636,7880,7901,7943,7998,8016,8052,8214,8453,8473,8595,8618,8696,8744,8829,8838,8884,9216,9488,9959,9975]
花费时间:137ms
2、查找电脑指定路径下所有以“.txt"结尾的文件,使用execute方法。
package com.su.mybatis.oracle.controller;import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;public class Test {public static void main(String[] args) {System.out.println("main 开始执行...");ForkJoinPool pool = new ForkJoinPool();SelectFilesTask task = new SelectFilesTask(new File("D:/360MoveData/"));pool.execute(task);System.out.println("main 正在执行...");try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}task.join();//插队,等待task完成System.out.println("main 结束...");}static class SelectFilesTask extends RecursiveAction {private static final long serialVersionUID = -2039880513818337443L;private File file;public SelectFilesTask(File file) {this.file = file;}@Overrideprotected void compute() {List<SelectFilesTask> tasks = new ArrayList<>();File[] files = file.listFiles();if (files!=null){for (File file : files) {if (file.isDirectory()) {//如果文件是子目录,对子目录都新建一个任务tasks.add(new SelectFilesTask(file));} else {//不是目录,判断是否是.txt结尾if (file.getAbsolutePath().endsWith(".txt")){System.out.println("文件:" + file.getAbsolutePath());}}}if (!tasks.isEmpty()) {// 在当前的 ForkJoinPool 上调度所有的子任务。for (SelectFilesTask subTask : invokeAll(tasks)) {subTask.join();}}}}}
}
输出结果:
main 开始执行...
main 正在执行...
文件:D:\360MoveData\Users\gx\Documents\sql修改.txt
文件:D:\360MoveData\Users\gx\Desktop\aaa.txt
文件:D:\360MoveData\Users\gx\Documents\修改.txt
文件:D:\360MoveData\Users\gx\Desktop\c.txt
文件:D:\360MoveData\Users\gx\Documents\草稿.txt
文件:D:\360MoveData\Users\gx\Desktop\docker命令.txt
.
.
.
文件:D:\360MoveData\Users\gx\Documents\Navicat\MySQL\logs\LogHistory.txt
文件:D:\360MoveData\Users\gx\Desktop\安装maven环境.txt
文件:D:\360MoveData\Users\gx\Desktop\电影.txt
文件:D:\360MoveData\Users\gx\Desktop\线上.txt
文件:D:\360MoveData\Users\gx\Documents\Navicat\Premium\logs\LogHistory.txt
文件:D:\360MoveData\Users\gx\Documents\NetSarang\Xshell 5 Update Log.txt
文件:D:\360MoveData\Users\gx\Desktop\jd-gui\readme.txt
文件:D:\360MoveData\Users\gx\Desktop\jd-gui\新建文本文档.txt
main 结束...
如果有写的不对的地方,请大家多多批评指正,非常感谢!
这篇关于ForkJoin的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!