码迷,mamicode.com
首页 > 其他好文 > 详细

分支合并框架

时间:2020-03-16 23:31:24      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:protected   框架   return   stat   dex   interrupt   join   exe   over   

 

0-100求和:
class
MyTask extends RecursiveTask<Integer>{ public static final int ADJAST_VALUE = 10; private int begin; private int end; private int result; public MyTask(int begin, int end) { this.begin = begin; this.end = end; } @Override protected Integer compute() { if((end-begin)<=ADJAST_VALUE){ for (int i = begin; i <=end ; i++) { result = result+i; } }else{ int mid =(begin+end)/2; MyTask myTask1 = new MyTask(begin,mid); MyTask myTask2 = new MyTask(mid+1,end); myTask1.fork(); myTask2.fork(); result = myTask1.join()+myTask2.join(); } return result; } } /** * 分支合并例子 * ForkJoinPool * ForkJoinTask * RecursiveTask */ public class ForkJoinDemo { public static void main(String[] args) throws ExecutionException, InterruptedException { MyTask myTask = new MyTask(0,100); ForkJoinPool forkJoinPool = new ForkJoinPool(); ForkJoinTask forkJoinTask = forkJoinPool.submit(myTask); System.out.println(forkJoinTask.get()); } }

 

分支合并框架

标签:protected   框架   return   stat   dex   interrupt   join   exe   over   

原文地址:https://www.cnblogs.com/hpdblogs/p/12507637.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!