首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
您找到你想要的搜索结果了吗?
是的
没有找到

JDK7并行计算框架介绍二 Fork/Join开发实例

package forktest; import java.util.*; import java.util.concurrent.RecursiveAction; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; import junit.*; public class SortTask extends RecursiveAction {     final long[] array;     final int hi;     final int lo;     private int THRESHOLD = 30;     //构造函数     public SortTask(long[] array)     {         this.array = array;         this.lo = 0;         this.hi = array.length - 1;             }     //构造函数     public SortTask(long[] array,int lo,int hi)     {         this.array = array;         this.lo = lo;         this.hi = hi;             }     //implement RecusiveTask must     protected void compute() {         if(hi - lo < THRESHOLD) {             sequentiallySort(array,lo,hi);                      }         else         {               int pivot = partition(array,lo,hi);             System.out.println("\npivot = " + pivot + ", low = " + lo + ", high = " + hi);             System.out.println("array" + Arrays.toString(array));             //注意此处接口有变化,老版本是coInvake,已不支持该接口             invokeAll(new SortTask(array,lo,pivot-1),new SortTask(array,pivot+1,hi));           }     }     //任务分割     private int partition(long[] array,int lo,int hi){         long x = array[hi];         int i = lo - 1;         for (int j = lo; j < hi; j++) {             if (array[j] <= x) {                  i++;                  swap(array, i, j);             }         }         swap(array, i + 1, hi);         return i+1;     }     //执行排序     private void sequentiallySort(long[] array,int lo,int hi){         Arrays.sort(array,lo,hi+1);     }     //数值交换     private void swap(long[] array,int i,int j){         if(i !=j)         {             long tmp = array[i];             array[i] = array[j];             array[j] = tmp;                     }     } }

02

扫码

添加站长 进交流群

领取专属 10元无门槛券

手把手带您无忧上云

扫码加入开发者社群

热门标签

活动推荐

    运营活动

    活动名称
    广告关闭
    领券