@@ -0,0 +1,1670 @@
+package com.hzh.corejava.concurrent.forkjoin;%0A%0Aimport java.util.concurrent.ExecutionException;%0Aimport java.util.concurrent.ForkJoinPool;%0Aimport java.util.concurrent.Future;%0Aimport java.util.concurrent.RecursiveTask;%0A%0Apublic class CountTask extends RecursiveTask%3CInteger%3E %7B%0A    private static final int THRESHOLD = 2;//%E9%98%88%E5%80%BC%0A    private int start;%0A    private int end;%0A%0A    public CountTask(int start, int end) %7B%0A        this.start = start;%0A        this.end = end;%0A    %7D%0A%0A%0A    @Override%0A    protected Integer compute() %7B%0A        int sum = 0;%0A        //%E5%A6%82%E6%9E%9C%E4%BB%BB%E5%8A%A1%E8%B6%B3%E5%A4%9F%E5%B0%8F%E5%B0%B1%E8%AE%A1%E7%AE%97%E4%BB%BB%E5%8A%A1%0A        boolean canCompute = (end - start) %3C= THRESHOLD;%0A        if (canCompute) %7B%0A            for (int i = start; i %3C= end; i++) %7B%0A                sum += i;%0A            %7D%0A        %7D else %7B%0A            //%E5%A6%82%E6%9E%9C%E4%BB%BB%E5%8A%A1%E5%A4%A7%E4%BA%8E%E9%98%80%E5%80%BC%EF%BC%8C%E5%B0%B1%E5%88%86%E8%A3%82%E6%88%90%E4%B8%A4%E4%B8%AA%E5%AD%90%E4%BB%BB%E5%8A%A1%E8%AE%A1%E7%AE%97%0A            int middle = (start + end) / 2;%0A            CountTask leftTask = new CountTask(start, middle);%0A            CountTask rightTask = new CountTask(middle + 1, end);%0A            //%E6%89%A7%E8%A1%8C%E5%AD%90%E4%BB%BB%E5%8A%A1%0A            leftTask.fork();%0A            rightTask.fork();%0A            //%E7%AD%89%E5%BE%85%E5%AD%90%E4%BB%BB%E5%8A%A1%E6%89%A7%E8%A1%8C%E5%AE%8C%EF%BC%8C%E5%B9%B6%E5%BE%97%E5%88%B0%E5%85%B6%E7%BB%93%E6%9E%9C%0A            int leftResult = leftTask.join();%0A            int rightResult = rightTask.join();%0A            //%E5%90%88%E5%B9%B6%E5%AD%90%E4%BB%BB%0A            sum = leftResult + rightResult;%0A        %7D%0A        return sum;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        ForkJoinPool forkJoinPool = new ForkJoinPool();%0A        //%E7%94%9F%E6%88%90%E4%B8%80%E4%B8%AA%E8%AE%A1%E7%AE%97%E4%BB%BB%E5%8A%A1%EF%BC%8C%E8%B4%9F%E8%B4%A3%E8%AE%A1%E7%AE%971+2+3+4%0A        CountTask task = new CountTask(1, 4);%0A        //%E6%89%A7%E8%A1%8C%E4%B8%80%E4%B8%AA%E4%BB%BB%E5%8A%A1%0A        Future result = forkJoinPool.submit(task);%0A        try %7B%0A            System.out.println(result.get());%0A        %7D catch (InterruptedException e) %7B%0A%0A        %7D catch (ExecutionException e) %7B%0A        %7D%0A    %7D%0A%7D%0A

添加Fork Join计数任务