0

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.

Rollerball
  • 12,618
  • 23
  • 92
  • 161

2 Answers2

0

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

edharned
  • 1,884
  • 1
  • 19
  • 20
0

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

Community
  • 1
  • 1
John Vint
  • 39,695
  • 7
  • 78
  • 108