温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何进行JDK7新特性中fork/join框架的原理分析

发布时间:2021-12-27 16:07:03 来源:亿速云 阅读:74 作者:柒染 栏目:编程语言

如何进行JDK7新特性中fork/join框架的原理分析,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

原理解析:fork分解,join结合。这个框架的本质是将一个任务分解成多个子任务,每个子任务用单独的线程去处理。这里用到了递归的思想。框架的结构图可以参考

如何进行JDK7新特性中fork/join框架的原理分析

使用fork/join 框架很简单,

1.实现子问题的一般求解算法

2.如何分解问题

3.继承 RecursiveAction ,实现compute()方法

伪代码代码

  Result solve(Problem problem) {     if (problem is small)         directly solve problem     else {         split problem into independent parts         fork new subtasks to solve each part         join all subtasks         compose result from subresults     }

这里我通过一个改进的二分查找来讲解fork/join的使用。(后面才发现,选用这个案例是非常失败的,因为二分查找的时间是logn,而创建线程的开销更大,这样并不能体现多线程二分查找的优势,所以这个代码不具有实用性,只是为了说明如何使用框架:)

代码如下:

BinarySearchProblem.java

Java代码

package testjdk7;         import java.util.Arrays;     /**     * @author kencs@foxmail.com     */    public class BinarySearchProblem {         private final int[] numbers;         private final int start;         private final int end;         public final int size;                  public BinarySearchProblem(int[] numbers,int start,int end){             this.numbers = numbers;             this.start = start;             this.end = end;             this.size = end -start;         }                  public int searchSequentially(int numberToSearch){            //偷懒,不自己写二分查找了            return Arrays.binarySearch(numbers, start, end, numberToSearch);         }                  public BinarySearchProblem subProblem(int subStart,int subEnd){             return new BinarySearchProblem(numbers,start+subStart,start+subEnd);         }     }

BiSearchWithForkJoin.java

Java代码

package testjdk7;     import java.util.concurrent.ForkJoinPool;     import java.util.concurrent.RecursiveAction;         /**     * @author kencs@foxmail.com     */    public class BiSearchWithForkJoin extends RecursiveAction {         private final int threshold;         private final BinarySearchProblem problem;         public int result;         private final int numberToSearch;                  public BiSearchWithForkJoin(BinarySearchProblem problem,int threshold,int numberToSearch){             this.problem = problem;             this.threshold = threshold;             this.numberToSearch = numberToSearch;         }             @Override        protected void compute() {            if(problem.size < threshold){ //小于阀值,就直接用普通的二分查找                result = problem.searchSequentially(numberToSearch);            }else{                //分解子任务                int midPoint = problem.size/2;                BiSearchWithForkJoin left = new BiSearchWithForkJoin(problem.subProblem(0, midPoint),threshold,numberToSearch);                BiSearchWithForkJoin right = new BiSearchWithForkJoin(problem.subProblem(midPoint+1, problem.size),threshold,numberToSearch);                invokeAll(left,right);                result = Math.max(left.result, right.result);            }         }                  //构造数据         private static final int[] data = new int[1000_0000];         static{             for(int i = 0;i<1000_0000;i++){                 data[i] = i;             }         }         public static void main(String[] args){            BinarySearchProblem problem = new BinarySearchProblem(data,0,data.length);            int threshold = 100;            int nThreads = 10;            //查找100_0000所在的下标            BiSearchWithForkJoin  bswfj = new BiSearchWithForkJoin(problem,threshold,100_0000);            ForkJoinPool fjPool = new ForkJoinPool(nThreads);            fjPool.invoke(bswfj);            System.out.printf("Result is:%d%n",bswfj.result);         }                       }

RecursiveTask 还可以带返回值,这里给出一段代码作为参考(斐波那契函数)

(来自http://www.ibm.com/developerworks/cn/java/j-lo-forkjoin/index.html)

Java代码

class Fibonacci extends RecursiveTask {         final int n;             Fibonacci(int n) {             this.n = n;         }             private int compute(int small) {             final int[] results = { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };             return results[small];         }             public Integer compute() {             if (n <= 10) {                 return compute(n);             }             Fibonacci f1 = new Fibonacci(n - 1);             Fibonacci f2 = new Fibonacci(n - 2);             System.out.println("fork new thread for " + (n - 1));             f1.fork();             System.out.println("fork new thread for " + (n - 2));             f2.fork();             return f1.join() + f2.join();         }     }

用途

只要问题能够分解成类似子问题的,都可以使用这个框架。对于大批量的数据尤其合适。

看完上述内容,你们掌握如何进行JDK7新特性中fork/join框架的原理分析的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI