Invoking more than 2 RecursiveActionTasks in compute()

54 views Asked by At

What would be the drawback to invoke many RecursiveAction in the following code?

class RecursiveActionThing extends RecursiveAction {

int numbers = 1000;
public RecursiveActionThing(int numbers)
{
    this.numbers = numbers;

}
public void compute()
{
    if (numbers<500)
    {
        for (int i =0;i<numbers;i++)
        {
            System.out.println(i);
        }
    }
    else{
        invokeAll(new RecursiveActionThing(numbers/2),new RecursiveActionThing(numbers/2), new RecursiveActionThing(numbers/2), new RecursiveActionThing(numbers/2));

    }
}

}

So far I have only seen invoking 2 tasks, so probably doing the above invocation is going to create a massive overhead in creating all those tasks, however why is it allowed by placing a varargs as paramenter? In certain situation might be useful? Which ones? Thanks in advance.

2

There are 2 answers

0
edharned On

There is a fine example of this in the test Classes supplied with jsr166, MatrixMultiply.java You can find it here

0
John Vint On

This doesn't make much sense. If you're looking to have a certain number executed you should be able to control it yourself through divide with intervals of 2.

however why is it allowed by placing a varargs as paramenter? In certain situation might be useful? Which ones?

I can't think of any useful reasons this would be used. This is a carry over of the ExecutorService and is just implementing the functionality. I wouldn't recommend it.

Take a look at this question. It doesn't act as well as you would hope.

ForkJoinPool seems to waste a thread