Keine Cache-Version

Caching deaktiviert Standardeinstellung für diese Seite:aktiviert (code LNG204)
Wenn die Anzeige zu langsam ist, können Sie den Benutzermodus deaktivieren, um die zwischengespeicherte Version anzuzeigen.
java.util.concurrent

Class RecursiveAction

  • All Implemented Interfaces:
    Serializable, Future<Void>

    public abstract class RecursiveAction
    extends ForkJoinTask<Void>
    A recursive resultless ForkJoinTask. This class establishes conventions to parameterize resultless actions as Void ForkJoinTasks. Because null is the only valid value of type Void, methods such as join always return null upon completion.

    Sample Usages. Here is a sketch of a ForkJoin sort that sorts a given long[] array:

     class SortTask extends RecursiveAction {
       final long[] array; final int lo; final int hi;
       SortTask(long[] array, int lo, int hi) {
         this.array = array; this.lo = lo; this.hi = hi;
       }
       protected void compute() {
         if (hi - lo < THRESHOLD)
           sequentiallySort(array, lo, hi);
         else {
           int mid = (lo + hi) >>> 1;
           invokeAll(new SortTask(array, lo, mid),
                     new SortTask(array, mid, hi));
           merge(array, lo, hi);
         }
       }
     }
    You could then sort anArray by creating new SortTask(anArray, 0, anArray.length-1) and invoking it in a ForkJoinPool. As a more concrete simple example, the following task increments each element of an array:
     class IncrementTask extends RecursiveAction {
       final long[] array; final int lo; final int 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));
         }
       }
     }

    The following example illustrates some refinements and idioms that may lead to better performance: RecursiveActions need not be fully recursive, so long as they maintain the basic divide-and-conquer approach. Here is a class that sums the squares of each element of a double array, by subdividing out only the right-hand-sides of repeated divisions by two, and keeping track of them with a chain of next references. It uses a dynamic threshold based on method getSurplusQueuedTaskCount, but counterbalances potential excess partitioning by directly performing leaf actions on unstolen tasks rather than further subdividing.

     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;
       }
     }
    Since:
    1.7
    See Also:
    Serialized Form
    • Constructor Detail

      • RecursiveAction

        public RecursiveAction()
    • Method Detail

      • compute

        protected abstract void compute()
        The main computation performed by this task.
      • setRawResult

        protected final void setRawResult(Void mustBeNull)
        Requires null completion value.
        Specified by:
        setRawResult in class ForkJoinTask<Void>
        Parameters:
        mustBeNull - the value
      • exec

        protected final boolean exec()
        Implements execution conventions for RecursiveActions.
        Specified by:
        exec in class ForkJoinTask<Void>
        Returns:
        true if completed normally

Deutsche Übersetzung

Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.

Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.

Vielen Dank im Voraus.

Dokument erstellt 11/06/2005, zuletzt geändert 04/03/2020
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/java-api-rf-java/util/concurrent/RecursiveAction.html

Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.

Referenzen

  1. Zeigen Sie - html-Dokument Sprache des Dokuments:fr Manuel PHP : https://docs.oracle.com

Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor Diese Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.

Inhaltsverzeichnis Haut