Java线程--ForkJoinPool使用
原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11871099.html
Java线程--ForkJoinPool使用
简单解释下:
Fork是执行的意思, Join是等待的意思, 结合使用就是先用Fork来执行子任务, 然后再用Join等待子任务全部执行完毕之后再统一处理或者返回 .
public static void main(String[] args) throws InterruptedException, ExecutionException { /** * 多线程框架 */ Executor executor = null; /** * Executor 的子接口 */ ExecutorService service = null; /** * ExecutorService 的抽象子类 */ AbstractExecutorService service1 = null; /** * AbstractExecutorService 的子类 * 执行由一个大任务拆分成的多个小任务 * 完成一个任务的分段执行, 最后再统一处理 * 配合 ForkJoinTask 使用 */ ForkJoinPool xxxx = new ForkJoinPool(); /** * 异步计算返回的结果 */ Future future = null; /** * Future的抽象子类 */ ForkJoinTask forkJoinTask = null; /** * 没有返回值, ForkJoinTask的抽象子类 */ RecursiveAction recursiveAction = null; /** * 有返回值, ForkJoinTask的抽象子类 */ RecursiveTask recursiveTask = null; /** * 来 300 个打印的任务 */ PrintTasks printTasks = new PrintTasks(0, 200); /** * 来一个任务存储器 */ ForkJoinPool forkJoinPool = new ForkJoinPool(); /** * 提交任务 */ forkJoinPool.submit(printTasks); /** * 等待所有任务完成 */ forkJoinPool.awaitTermination(20, TimeUnit.SECONDS); forkJoinPool.shutdown(); int[] intArr = new int[100]; Random random = new Random(); int total = 0; int len = intArr.length; for (int i = 0; i < len ; i++) { int tmp = random.nextInt(5); total += (intArr[i] = tmp); } System.out.println("初始化时的数组总和 : " + total); SumTask sumTask = new SumTask(intArr, 0, len); /** * 通用池 */ ForkJoinPool pool = new ForkJoinPool(8); Future<Integer> future1 = pool.invoke(sumTask); System.out.println("任务执行结果: " + future1.get()); pool.shutdown(); }
/** * 继承 RecursiveAction 来实现任务可分解 */ class PrintTasks extends RecursiveAction { private static final int THRESHOLD = 20; // 设置一个最多打印的数 private int start; private int end; public PrintTasks(int start, int end) { super(); 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; PrintTasks left = new PrintTasks(start, middle); PrintTasks right = new PrintTasks(middle, end); /** * fork() 就是执行的意思left.fork();right.fork(); * 这里并行执行以下 */ invokeAll(left, right); } } }
/** * 统计总数的任务 */ class SumTask extends RecursiveTask<Integer> { /** * 每个小任务 最多只累加5个数 */ private static final int THRESHOLD = 5; /** * 数据的数组 */ private int arry[]; private int start; private int end; public SumTask(int[] arry, int start, int end) { super(); this.arry = arry; this.start = start; this.end = end; } /** * 类种是什么类型的值就返回什么类型的值 * @return */ @Override protected Integer compute() { int sum =0; /** * 当end与start之间的差小于设定任务数量时,才开始进行实际的累加 */ if(end - start <THRESHOLD){ for(int i= start;i<end;i++){ sum += arry[i]; } return sum; }else { /** * 任务数量超过规定执行数量时 , 进行二分法拆分 */ int middle = (start+ end)/2; SumTask small = new SumTask(arry, start, middle); SumTask big = new SumTask(arry, middle, end); /** * 执行任务 small.fork(); big.fork(); */ invokeAll(small, big); /** * 把各个小任务累加的结果合并起来并且返回 * join() 会等待子任务执行完并得到其结果 */ return small.join()+big.join(); } } }
打印结果如下:
ForkJoinPool-2-worker-1的i : 187 ForkJoinPool-2-worker-2的i : 87 ForkJoinPool-2-worker-2的i : 88 ForkJoinPool-2-worker-2的i : 89 ForkJoinPool-2-worker-2的i : 90 ForkJoinPool-2-worker-2的i : 91 ForkJoinPool-2-worker-2的i : 92 ForkJoinPool-2-worker-2的i : 93 ForkJoinPool-2-worker-2的i : 94 ForkJoinPool-2-worker-2的i : 95 ForkJoinPool-2-worker-2的i : 96 ForkJoinPool-2-worker-2的i : 97 ForkJoinPool-2-worker-2的i : 98 ForkJoinPool-2-worker-2的i : 99 ForkJoinPool-2-worker-2的i : 75 ForkJoinPool-2-worker-2的i : 76 ForkJoinPool-2-worker-2的i : 77 ForkJoinPool-2-worker-2的i : 78 ForkJoinPool-2-worker-2的i : 79 ForkJoinPool-2-worker-2的i : 80 ForkJoinPool-2-worker-2的i : 81 ForkJoinPool-2-worker-2的i : 82 ForkJoinPool-2-worker-2的i : 83 ForkJoinPool-2-worker-2的i : 84 ForkJoinPool-2-worker-2的i : 85 ForkJoinPool-2-worker-2的i : 86 ForkJoinPool-2-worker-2的i : 62 ForkJoinPool-2-worker-2的i : 63 ForkJoinPool-2-worker-2的i : 64 ForkJoinPool-2-worker-2的i : 65 ForkJoinPool-2-worker-2的i : 66 ForkJoinPool-2-worker-2的i : 67 ForkJoinPool-2-worker-2的i : 68 ForkJoinPool-2-worker-2的i : 69 ForkJoinPool-2-worker-2的i : 70 ForkJoinPool-2-worker-2的i : 71 ForkJoinPool-2-worker-2的i : 72 ForkJoinPool-2-worker-2的i : 73 ForkJoinPool-2-worker-2的i : 74 ForkJoinPool-2-worker-2的i : 50 ForkJoinPool-2-worker-2的i : 51 ForkJoinPool-2-worker-2的i : 52 ForkJoinPool-2-worker-2的i : 53 ForkJoinPool-2-worker-2的i : 54 ForkJoinPool-2-worker-2的i : 55 ForkJoinPool-2-worker-1的i : 188 ForkJoinPool-2-worker-1的i : 189 ForkJoinPool-2-worker-1的i : 190 ForkJoinPool-2-worker-1的i : 191 ForkJoinPool-2-worker-1的i : 192 ForkJoinPool-2-worker-1的i : 193 ForkJoinPool-2-worker-1的i : 194 ForkJoinPool-2-worker-1的i : 195 ForkJoinPool-2-worker-1的i : 196 ForkJoinPool-2-worker-1的i : 197 ForkJoinPool-2-worker-1的i : 198 ForkJoinPool-2-worker-1的i : 199 ForkJoinPool-2-worker-1的i : 175 ForkJoinPool-2-worker-1的i : 176 ForkJoinPool-2-worker-1的i : 177 ForkJoinPool-2-worker-1的i : 178 ForkJoinPool-2-worker-1的i : 179 ForkJoinPool-2-worker-1的i : 180 ForkJoinPool-2-worker-1的i : 181 ForkJoinPool-2-worker-1的i : 182 ForkJoinPool-2-worker-1的i : 183 ForkJoinPool-2-worker-1的i : 184 ForkJoinPool-2-worker-1的i : 185 ForkJoinPool-2-worker-1的i : 186 ForkJoinPool-2-worker-1的i : 162 ForkJoinPool-2-worker-1的i : 163 ForkJoinPool-2-worker-1的i : 164 ForkJoinPool-2-worker-1的i : 165 ForkJoinPool-2-worker-1的i : 166 ForkJoinPool-2-worker-1的i : 167 ForkJoinPool-2-worker-1的i : 168 ForkJoinPool-2-worker-1的i : 169 ForkJoinPool-2-worker-1的i : 170 ForkJoinPool-2-worker-1的i : 171 ForkJoinPool-2-worker-1的i : 172 ForkJoinPool-2-worker-1的i : 173 ForkJoinPool-2-worker-1的i : 174 ForkJoinPool-2-worker-1的i : 150 ForkJoinPool-2-worker-1的i : 151 ForkJoinPool-2-worker-1的i : 152 ForkJoinPool-2-worker-1的i : 153 ForkJoinPool-2-worker-1的i : 154 ForkJoinPool-2-worker-2的i : 56 ForkJoinPool-2-worker-2的i : 57 ForkJoinPool-2-worker-2的i : 58 ForkJoinPool-2-worker-2的i : 59 ForkJoinPool-2-worker-2的i : 60 ForkJoinPool-2-worker-2的i : 61 ForkJoinPool-2-worker-2的i : 37 ForkJoinPool-2-worker-2的i : 38 ForkJoinPool-2-worker-2的i : 39 ForkJoinPool-2-worker-2的i : 40 ForkJoinPool-2-worker-2的i : 41 ForkJoinPool-2-worker-2的i : 42 ForkJoinPool-2-worker-2的i : 43 ForkJoinPool-2-worker-2的i : 44 ForkJoinPool-2-worker-2的i : 45 ForkJoinPool-2-worker-2的i : 46 ForkJoinPool-2-worker-2的i : 47 ForkJoinPool-2-worker-2的i : 48 ForkJoinPool-2-worker-2的i : 49 ForkJoinPool-2-worker-2的i : 25 ForkJoinPool-2-worker-2的i : 26 ForkJoinPool-2-worker-2的i : 27 ForkJoinPool-2-worker-2的i : 28 ForkJoinPool-2-worker-2的i : 29 ForkJoinPool-2-worker-2的i : 30 ForkJoinPool-2-worker-2的i : 31 ForkJoinPool-2-worker-2的i : 32 ForkJoinPool-2-worker-2的i : 33 ForkJoinPool-2-worker-2的i : 34 ForkJoinPool-2-worker-2的i : 35 ForkJoinPool-2-worker-2的i : 36 ForkJoinPool-2-worker-2的i : 12 ForkJoinPool-2-worker-2的i : 13 ForkJoinPool-2-worker-2的i : 14 ForkJoinPool-2-worker-2的i : 15 ForkJoinPool-2-worker-2的i : 16 ForkJoinPool-2-worker-2的i : 17 ForkJoinPool-2-worker-2的i : 18 ForkJoinPool-2-worker-2的i : 19 ForkJoinPool-2-worker-2的i : 20 ForkJoinPool-2-worker-2的i : 21 ForkJoinPool-2-worker-2的i : 22 ForkJoinPool-2-worker-2的i : 23 ForkJoinPool-2-worker-2的i : 24 ForkJoinPool-2-worker-2的i : 0 ForkJoinPool-2-worker-2的i : 1 ForkJoinPool-2-worker-2的i : 2 ForkJoinPool-2-worker-2的i : 3 ForkJoinPool-2-worker-2的i : 4 ForkJoinPool-2-worker-2的i : 5 ForkJoinPool-2-worker-2的i : 6 ForkJoinPool-2-worker-2的i : 7 ForkJoinPool-2-worker-2的i : 8 ForkJoinPool-2-worker-2的i : 9 ForkJoinPool-2-worker-2的i : 10 ForkJoinPool-2-worker-2的i : 11 ForkJoinPool-2-worker-2的i : 137 ForkJoinPool-2-worker-2的i : 138 ForkJoinPool-2-worker-2的i : 139 ForkJoinPool-2-worker-2的i : 140 ForkJoinPool-2-worker-2的i : 141 ForkJoinPool-2-worker-2的i : 142 ForkJoinPool-2-worker-2的i : 143 ForkJoinPool-2-worker-2的i : 144 ForkJoinPool-2-worker-2的i : 145 ForkJoinPool-2-worker-2的i : 146 ForkJoinPool-2-worker-2的i : 147 ForkJoinPool-2-worker-2的i : 148 ForkJoinPool-2-worker-2的i : 149 ForkJoinPool-2-worker-2的i : 125 ForkJoinPool-2-worker-2的i : 126 ForkJoinPool-2-worker-2的i : 127 ForkJoinPool-2-worker-2的i : 128 ForkJoinPool-2-worker-2的i : 129 ForkJoinPool-2-worker-2的i : 130 ForkJoinPool-2-worker-2的i : 131 ForkJoinPool-2-worker-2的i : 132 ForkJoinPool-2-worker-2的i : 133 ForkJoinPool-2-worker-2的i : 134 ForkJoinPool-2-worker-2的i : 135 ForkJoinPool-2-worker-2的i : 136 ForkJoinPool-2-worker-2的i : 112 ForkJoinPool-2-worker-2的i : 113 ForkJoinPool-2-worker-2的i : 114 ForkJoinPool-2-worker-2的i : 115 ForkJoinPool-2-worker-2的i : 116 ForkJoinPool-2-worker-2的i : 117 ForkJoinPool-2-worker-2的i : 118 ForkJoinPool-2-worker-2的i : 119 ForkJoinPool-2-worker-2的i : 120 ForkJoinPool-2-worker-2的i : 121 ForkJoinPool-2-worker-2的i : 122 ForkJoinPool-2-worker-2的i : 123 ForkJoinPool-2-worker-2的i : 124 ForkJoinPool-2-worker-2的i : 100 ForkJoinPool-2-worker-2的i : 101 ForkJoinPool-2-worker-2的i : 102 ForkJoinPool-2-worker-2的i : 103 ForkJoinPool-2-worker-2的i : 104 ForkJoinPool-2-worker-2的i : 105 ForkJoinPool-2-worker-2的i : 106 ForkJoinPool-2-worker-2的i : 107 ForkJoinPool-2-worker-2的i : 108 ForkJoinPool-2-worker-2的i : 109 ForkJoinPool-2-worker-2的i : 110 ForkJoinPool-2-worker-2的i : 111 ForkJoinPool-2-worker-1的i : 155 ForkJoinPool-2-worker-1的i : 156 ForkJoinPool-2-worker-1的i : 157 ForkJoinPool-2-worker-1的i : 158 ForkJoinPool-2-worker-1的i : 159 ForkJoinPool-2-worker-1的i : 160 ForkJoinPool-2-worker-1的i : 161 初始化时的数组总和 : 204 任务执行结果: 204