分支合併框架

 /**     * 分支合併框架     */    public static void main(String[] args) throws Exception {        MyTask myTask=new MyTask(0,100);        ForkJoinPool pool=new ForkJoinPool();//建立一個池子        ForkJoinTask<Integer> submit = pool.submit(myTask);        System.out.println(submit.get());        pool.shutdown();    }}class MyTask extends RecursiveTask<Integer>{    private static final Integer ADJUST_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)<=ADJUST_VALUE){            for (int i=begin;i<=end;i++){                result+=i;            }        }else {            int middle=(end+begin)/2;            MyTask task=new MyTask(begin,middle);            MyTask task1=new MyTask(middle+1,end);            task.fork();            task1.fork();            result=task.join()+task1.join();        }        return result;    }
相關文章
相關標籤/搜索