如何在类列表上的Compute()方法中进行迭代扩展RecursiveAction / RecursiveTask

时间:2014-03-02 10:27:52

标签: java multithreading fork-join

我尝试了很多方法来使下面的场景工作,结果是无限的打印fork fork fork ..我试图调试,但它总是在task.join();等待很长时间没有结果。我理解fork / join的概念,我可以使用它,当我有任务时可以分为子部分,如:Fibonacci和Maximum of arrays。这里的场景是不同的意义,我必须在计算中迭代,而不是递归。有人可以帮忙吗?

CompositePoolTest

 import java.util.Random;
 import java.util.concurrent.ForkJoinPool;

 public class CompositePoolTest {

Random random = new Random(123);
int done = 0;
int rest= 0;
int tt = 4;
ForkJoinPool pool = new ForkJoinPool(tt);
int M= 1000;
int N = 1000;

public static void main(String[] args) {
    new CompositePoolTest().compute();
}

private void compute() {
    double[][] original_matrix = new double[M][N];
    original_matrix = radom_intialization();
    double[][] temp_matrix = new double[M][N];
    done= 0;
    rest= (M * N - done) / (tt- 0);
    DynamicCompositeFinder dynamicFinder = new DynamicCompositeFinder(done,rest,original_matrix,temp_matrix);
    new ForkJoinPool().invoke(dynamicFinder);
}

private double[][] radom_intialization() {
    double [][] grid_matrix = new double[M][N];
    for (int i = 0; i < M; i++)
        for (int j = 0; j < N; j++) {
            grid_matrix[i][j] = random.nextDouble()+0.10;
        }
    return grid_matrix;
}
 }

DynamicCompositeFinder

package test;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;

    public class DynamicCompositeFinder extends RecursiveAction {
int done = 0;
int rest = 0;
int pp = 4;
ForkJoinPool pool = new ForkJoinPool(pp);
// Matrix dimensions
int M = 1000;
int N = 1000;
int x = 0;
int y = 0;
int niteration = 150;
double[][] original_matrix = new double[M][N];
double[][] temp_matrix = new double[M][N];

public DynamicCompositeFinder(int done, int rest, double[][] original_matrix, double[][] temp_matrix) {
    this.done = done;
    this.rest = rest;
    this.original_matrix = original_matrix;
    this.temp_matrix = temp_matrix;

    int limit = done + rest;
    for (int i = done; i < limit; i++) {
        x = i / M;
        y = i % M;
        temp_matrix[x][y] = fun_calculation(x, y, original_matrix);
    }
}

private double fun_calculation(int x2, int y2, double[][] original_matrix2) {
     double temp = 2 * (original_matrix2[x][y] );
    return temp;
}

@Override
protected void compute() {
    for (int i = 0; i < niteration; i++) {
        done = 0;
        List<RecursiveAction> forks = new LinkedList<RecursiveAction>();
        for (int p = 0; p < pp; p++) // n is predefined n = 9
        {
            rest = (M * N - done) / (pp - p);
            DynamicCompositeFinder finder = new DynamicCompositeFinder(done, rest, original_matrix, temp_matrix);
            p++;
            forks.add((RecursiveAction) finder.fork());
            System.out.println("Fork-" + Thread.currentThread().getName()
                    + "  State: " + Thread.currentThread().getState());
        }

        for (RecursiveAction task : forks) {
            task.join();
            System.out.println("Join-" + Thread.currentThread().getName()
                    + "  State: " + Thread.currentThread().getState());
        }
        original_matrix = copy_matrix(temp_matrix);
    }
}

public double[][] copy_matrix(double [][] matrix)
{
    double [][] out= new double [matrix.length][matrix[0].length];
    for(int i=0;i<matrix.length;i++)
    {
        out[i]= matrix[i].clone();
    }
    return out;
}}

输出

Fork-ForkJoinPool-1-worker-1  State: RUNNABLE
Fork-ForkJoinPool-1-worker-1  State: RUNNABLE
Fork-ForkJoinPool-1-worker-2  State: RUNNABLE
Fork-ForkJoinPool-1-worker-2  State: RUNNABLE
Fork-ForkJoinPool-1-worker-3  State: RUNNABLE
Fork-ForkJoinPool-1-worker-4  State: RUNNABLE
Fork-ForkJoinPool-1-worker-4  State: RUNNABLE
Fork-ForkJoinPool-1-worker-3  State: RUNNABLE
Fork-ForkJoinPool-1-worker-5  State: RUNNABLE
Fork-ForkJoinPool-1-worker-5  State: RUNNABLE
Fork-ForkJoinPool-1-worker-6  State: RUNNABLE
Fork-ForkJoinPool-1-worker-6  State: RUNNABLE
Fork-ForkJoinPool-1-worker-7  State: RUNNABLE
Fork-ForkJoinPool-1-worker-7  State: RUNNABLE
Fork-ForkJoinPool-1-worker-8  State: RUNNABLE
Fork-ForkJoinPool-1-worker-8  State: RUNNABLE
Fork-ForkJoinPool-1-worker-9  State: RUNNABLE
Fork-ForkJoinPool-1-worker-9  State: RUNNABLE
Fork-ForkJoinPool-1-worker-10  State: RUNNABLE
Fork-ForkJoinPool-1-worker-10  State: RUNNABLE
Fork-ForkJoinPool-1-worker-11  State: RUNNABLE
Fork-ForkJoinPool-1-worker-11  State: RUNNABLE
Fork-ForkJoinPool-1-worker-12  State: RUNNABLE
Fork-ForkJoinPool-1-worker-12  State: RUNNABLE
Fork-ForkJoinPool-1-worker-13  State: RUNNABLE
.....
......

1 个答案:

答案 0 :(得分:0)

主要问题是你永远fork()。没有塞子代码,例如: if(computed&lt; limiter)return; 因此,您将任务添加到双端队列,线程将永久地获取每个任务并分叉更多任务。我为你的代码添加了一个塞子并在Java7中运行它。 join()被调用,但外部迭代继续进行。所以你有一些逻辑问题。

第二个问题是你误解了F / J框架。该框架不是通用并行引擎。它是专门设计用于递归地沿着平衡树的叶子行走的学术代码(D.A.G.)因为您没有平衡树,所以您无法根据JavaDoc中给出的示例进行处理: 分开左,右; left.fork(); right.compute(); left.join(); 而且你没有进行递归分解。您的代码更适合Java8的CountedCompler()