public abstract class RecursiveAction extends ForkJoinTask<Void>
ForkJoinTask。 このクラスは、Void ForkJoinTaskのような結果の出ないアクションをパラメータ化するための規則を確立します。 nullはVoid型の唯一の有効な値であるため、joinなどのメソッドは完了時に常にnullを返します。 
 使用例。 ここでは、指定されたlong[]配列をソートする簡単だが完全なForkJoinソートを示します。 
  
 
 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));
     }
   }
 }
 次の例は、パフォーマンスを向上させる可能性のある調整や方法のいくつかを示しています。RecursiveActionは、基本的な分割統治法を維持しているかぎり、完全に再帰的である必要はありません。 ここでは、繰返し除算の右辺のみを2で分割し、それらをnext参照のチェーンで追跡することによってdouble配列の各要素の2乗を合計するクラスを示します。 これは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;
   }
 }| コンストラクタ | 説明 | 
|---|---|
| RecursiveAction() | 
| 修飾子と型 | メソッド | 説明 | 
|---|---|---|
| protected abstract void | compute() | このタスクによって実行される主な計算です。 | 
| protected boolean | exec() | RecursiveActionsのための実行規則を実装します。 | 
| Void | getRawResult() | 常に nullを返します。 | 
| protected void | setRawResult(Void mustBeNull) | null完了値が必要です。 | 
adapt, adapt, adapt, cancel, compareAndSetForkJoinTaskTag, complete, completeExceptionally, fork, get, get, getException, getForkJoinTaskTag, getPool, getQueuedTaskCount, getSurplusQueuedTaskCount, helpQuiesce, inForkJoinPool, invoke, invokeAll, invokeAll, invokeAll, isCancelled, isCompletedAbnormally, isCompletedNormally, isDone, join, peekNextLocalTask, pollNextLocalTask, pollTask, quietlyComplete, quietlyInvoke, quietlyJoin, reinitialize, setForkJoinTaskTag, tryUnforkprotected abstract void compute()
public final Void getRawResult()
nullを返します。getRawResult、クラスForkJoinTask<Void>null (常時)protected final void setRawResult(Void mustBeNull)
setRawResult、クラスForkJoinTask<Void>mustBeNull - 値protected final boolean exec()
exec、クラスForkJoinTask<Void>true バグまたは機能を送信 
詳細なAPIリファレンスおよび開発者ドキュメントについては、Java SEのドキュメントを参照してください。 そのドキュメントには、概念的な概要、用語の定義、回避方法、有効なコード例などの、開発者を対象にしたより詳細な説明が含まれています。 
 Copyright © 1993, 2025, Oracle and/or its affiliates.  All rights reserved.  Use is subject to license terms.  Documentation Redistribution Policyも参照してください。