这篇文章主要为大家详细介绍了Java通过Fork、Join来优化并行计算,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Java通过Fork/Join优化并行计算的具体代码,供大家参考,具体内容如下
Java代码:
package Threads; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveAction; /** * Created by Frank */ public class RecursiveActionDemo extends RecursiveAction { static int[] raw = {19, 3, 0, -1, 57, 24, 65, Integer.MAX_VALUE, 42, 0, 3, 5}; static int[] sorted = null; int[] source; int[] dest; int length; int start; final static int THRESHOLD = 4; public static void main(String[] args) { sorted = new int[raw.length]; ForkJoinPool pool = new ForkJoinPool(); pool.invoke(new RecursiveActionDemo(raw, 0, raw.length, sorted)); System.out.println('['); for (int i : sorted) { System.out.println(i + ","); } System.out.println(']'); } public RecursiveActionDemo(int[] source, int start, int length, int[] dest) { this.source = source; this.dest = dest; this.length = length; this.start = start; } @Override protected void compute() { System.out.println("ForkJoinDemo.compute()"); if (length