當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Java.util.concurrent.RecursiveAction用法及代碼示例


RecursiveAction是一個抽象類,封裝了一個不返回結果的任務。它是 ForkJoinTask 的子類,ForkJoinTask 是一個抽象類,表示可以在多核係統中的單獨核上執行的任務。 RecursiveAction 類經過擴展以創建具有 void 返回類型的任務。表示任務計算部分的代碼保存在 RecursiveAction 的 compute() 方法中。

RecursiveAction用於可以劃分並並行執行的任務。這些任務不應返回任何值。例如,可以使用 RecursiveAction 輕鬆實現對大型數組的排序,將數組劃分為可管理的小塊,並且每個部分在單獨的核心上進行排序。

類層次結構

java.lang.Object
↳ java.util.concurrent.ForkJoinTask<Void>
  ↳ java.util.concurrent.RecursiveAction

遞歸操作的構造函數:

  1. RecursiveAction:使用默認設置創建RecursiveAction的對象。

    用法:

    public RecursiveAction()
    

方法

  1. compute()- 它是執行任務執行的計算的方法。

    用法:

    protected abstract void compute()
    
  2. exec()- 此方法實現執行 RecursiveAction 任務所需的基本規則。

    用法:

    protected final boolean exec()
    
  3. getRawResult()- 該函數返回任務完成狀態。它總是返回 null。

    用法:

    public final Void getRawResult()
    
  4. setRawResult()- 該函數將任務完成狀態設置為參數中傳遞的值。

    用法:

    protected final void setRawResult(Void mustBeNull)
    

示例:演示RecursiveAction類


// Java program to demonstrate RecursiveAction Class 
  
import java.util.concurrent.ForkJoinPool; 
import java.util.concurrent.RecursiveAction; 
  
public class ForkJoinDemo { 
    public static void main(String[] args) 
    { 
        // Create a pool of threads. 
        ForkJoinPool fjp = new ForkJoinPool(); 
        double[] nums = new double[100000]; 
  
        // Give nums some values 
        for (int i = 0; i < nums.length; i++) { 
            nums[i] = (double)i; 
        } 
        System.out.println("A portion of the original sequence"); 
        for (int i = 0; i < 9; i++) { 
            System.out.print(nums[i] + " "); 
        } 
        System.out.println(); 
        SqrtTransform task 
            = new SqrtTransform(nums, 0, nums.length); 
  
        // Start the task 
        fjp.invoke(task); 
        System.out.println("A portion of the transformed sequence"
                           + " (to four decimal places): "); 
        for (int i = 0; i < 9; i++) { 
            System.out.printf("%.4f ", nums[i]); 
        } 
        System.out.println(); 
    } 
} 
  
// A task that transforms the elements into their square roots 
class SqrtTransform extends RecursiveAction { 
    final int seqThreshold = 1000; 
  
    double[] data; 
  
    // Determines what part of data to process 
    int start, end; 
  
    SqrtTransform(double[] data, int start, int end) 
    { 
        this.data = data; 
        this.start = start; 
        this.end = end; 
    } 
  
    // The method where parallel computation will occur 
    @Override
    protected void compute() 
    { 
        // If the number of elements are less 
        // than the sequential threshold 
        if ((end - start) < seqThreshold) { 
            for (int i = start; i < end; i++) { 
                data[i] = Math.sqrt(data[i]); 
            } 
        } 
        else { 
            // Otherwise, continue to break the data into smaller pieces 
            // Find the midpoint 
            int middle = (start + end) / 2; 
  
            // Invoke new tasks, using the subdivided tasks. 
            invokeAll(new SqrtTransform(data, start, middle), 
                      new SqrtTransform(data, middle, end)); 
        } 
    } 
} 
輸出:

A portion of the original sequence
0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 
A portion of the transformed sequence (to four decimal places): 
0.0000 1.0000 1.4142 1.7321 2.0000 2.2361 2.4495 2.6458 2.8284 

參考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/RecursiveAction.html



相關用法


注:本文由純淨天空篩選整理自CharchitKapoor大神的英文原創作品 Java.util.concurrent.RecursiveAction class in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。