标签:prot test rup package proc except 实现类 实现 join
Java7提供了Fork/Join来支持将一个任务拆分成多个“小任务”并行计算,再把多个“小任务”的结果合并成总的计算结果。
package org.github.lujiango; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveAction; import java.util.concurrent.TimeUnit; class PrintTask extends RecursiveAction { private static final long serialVersionUID = 1L; private static final int threshold = 50; private int start; private int end; public PrintTask(int start, int end) { this.start = start; this.end = end; } @Override protected void compute() { if (end - start < threshold) { for (int i = start; i < end; i++) { System.out.println(Thread.currentThread().getName() + " i: " + i); } } else { int middle = (start + end) / 2; PrintTask left = new PrintTask(start, middle); PrintTask right = new PrintTask(middle, end); left.fork(); right.fork(); } } } public class Test20 { public static void main(String[] args) throws InterruptedException { ForkJoinPool pool = new ForkJoinPool(); pool.submit(new PrintTask(0, 300)); pool.awaitTermination(2, TimeUnit.SECONDS); pool.shutdown(); } }
分解后的任务分别调用fork()方法开始并行执行。
package org.github.lujiango; import java.util.Random; import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.Future; import java.util.concurrent.RecursiveTask; class CalTask extends RecursiveTask<Integer> { private static final long serialVersionUID = 1L; private static final int threshold = 20; private int[] arr; private int start; private int end; public CalTask(int[] arr, int start, int end) { this.arr = arr; this.start = start; this.end = end; } @Override protected Integer compute() { int sum = 0; if (end - start < threshold) { for (int i = start; i < end; i++) { sum += arr[i]; } return sum; } else { int middle = (start + end) / 2; CalTask left = new CalTask(arr, start, middle); CalTask right = new CalTask(arr, middle, end); left.fork(); right.fork(); return left.join() + right.join(); } } } public class Test21 { public static void main(String[] args) throws InterruptedException, ExecutionException { int[] arr = new int[100]; Random random = new Random(); int total = 0; for (int i = 0, len = arr.length; i < len; i++) { int tmp = random.nextInt(20); total += (arr[i] = tmp); } System.out.println(total); ForkJoinPool pool = new ForkJoinPool(); Future<Integer> future = pool.submit(new CalTask(arr, 0, arr.length)); System.out.println(future.get()); pool.shutdown(); } }
标签:prot test rup package proc except 实现类 实现 join
原文地址:http://www.cnblogs.com/lujiango/p/7581048.html