模块 java.base

类 RecursiveAction

java.lang.Object
java.util.concurrent.ForkJoinTask <Void >
java.util.concurrent.RecursiveAction
所有已实现的接口:
Serializable , Future<Void>

public abstract class RecursiveAction extends ForkJoinTask <Void >
递归无结果 ForkJoinTask 。此类建立了将无结果操作参数化为 Void ForkJoinTask s 的约定。因为 nullVoid 类型的唯一有效值,所以 join 等方法在完成时总是返回 null

示例用法。这是一个简单但完整的 ForkJoin 排序,它对给定的 long[] 数组进行排序:

 
 static class SortTask extends RecursiveAction {
  final long[] array; final int lo, hi;
  SortTask(long[] array, int lo, int hi) {
   this.array = array; this.lo = lo; this.hi = hi;
  }
  SortTask(long[] array) { this(array, 0, array.length); }
  protected void compute() {
   if (hi - lo < THRESHOLD)
    sortSequentially(lo, hi);
   else {
    int mid = (lo + hi) >>> 1;
    invokeAll(new SortTask(array, lo, mid),
         new SortTask(array, mid, hi));
    merge(lo, mid, hi);
   }
  }
  // implementation details follow:
  static final int THRESHOLD = 1000;
  void sortSequentially(int lo, int hi) {
   Arrays.sort(array, lo, hi);
  }
  void merge(int lo, int mid, int hi) {
   long[] buf = Arrays.copyOfRange(array, lo, mid);
   for (int i = 0, j = lo, k = mid; i < buf.length; j++)
    array[j] = (k == hi || buf[i] < array[k]) ?
     buf[i++] : array[k++];
  }
 } 
然后您可以通过创建 new SortTask(anArray) 并在 ForkJoinPool 中调用它来对 anArray 进行排序。作为一个更具体的简单示例,以下任务递增数组的每个元素:
 
 class IncrementTask extends RecursiveAction {
  final long[] array; final int lo, hi;
  IncrementTask(long[] array, int lo, int hi) {
   this.array = array; this.lo = lo; this.hi = hi;
  }
  protected void compute() {
   if (hi - lo < THRESHOLD) {
    for (int i = lo; i < hi; ++i)
     array[i]++;
   }
   else {
    int mid = (lo + hi) >>> 1;
    invokeAll(new IncrementTask(array, lo, mid),
         new IncrementTask(array, mid, hi));
   }
  }
 } 

以下示例说明了一些可能会带来更好性能的改进和习惯用法: RecursiveActions 不需要完全递归,只要它们保持基本的分而治之方法即可。这是一个类,通过仅将重复除以 2 的右侧细分,并使用 next 引用链跟踪它们,对双精度数组的每个元素的平方求和。它使用基于方法 getSurplusQueuedTaskCount 的动态阈值,但通过直接对未窃取任务执行叶操作而不是进一步细分来抵消潜在的过度分区。

 
 double sumOfSquares(ForkJoinPool pool, double[] array) {
  int n = array.length;
  Applyer a = new Applyer(array, 0, n, null);
  pool.invoke(a);
  return a.result;
 }

 class Applyer extends RecursiveAction {
  final double[] array;
  final int lo, hi;
  double result;
  Applyer next; // keeps track of right-hand-side tasks
  Applyer(double[] array, int lo, int hi, Applyer next) {
   this.array = array; this.lo = lo; this.hi = hi;
   this.next = next;
  }

  double atLeaf(int l, int h) {
   double sum = 0;
   for (int i = l; i < h; ++i) // perform leftmost base step
    sum += array[i] * array[i];
   return sum;
  }

  protected void compute() {
   int l = lo;
   int h = hi;
   Applyer right = null;
   while (h - l > 1 && getSurplusQueuedTaskCount() <= 3) {
    int mid = (l + h) >>> 1;
    right = new Applyer(array, mid, h, right);
    right.fork();
    h = mid;
   }
   double sum = atLeaf(l, h);
   while (right != null) {
    if (right.tryUnfork()) // directly calculate if not stolen
     sum += right.atLeaf(right.lo, right.hi);
    else {
     right.join();
     sum += right.result;
    }
    right = right.next;
   }
   result = sum;
  }
 } 
自从:
1.7
参见:
  • 构造方法详细信息

    • RecursiveAction

      public RecursiveAction()
      子类调用的构造方法。
  • 方法详情

    • compute

      protected abstract void compute()
      此任务执行的主要计算。
    • getRawResult

      public final Void  getRawResult()
      总是返回 null
      指定者:
      getRawResult 在类 ForkJoinTask<Void>
      返回:
      null总是
    • setRawResult

      protected final void setRawResult(Void  mustBeNull)
      需要空完成值。
      指定者:
      setRawResult 在类 ForkJoinTask<Void>
      参数:
      mustBeNull - 值
    • exec

      protected final boolean exec()
      实现 RecursiveActions 的执行约定。
      指定者:
      exec 在类 ForkJoinTask<Void>
      返回:
      true 如果已知此任务已正常完成