1

I have to use Java Recursive Task (Fork and Join) to calculate something like this: (3*3)^2.

I have this code which is supposed to work:

public class ForkJoin1 extends RecursiveTask<Long> {
    int num;
    public ForkJoin1 (int num) {
        this.num = num;
    }
    @Override
    protected Long compute() {
        if(num == 0) return Long.valueOf(1);
        ForkJoin1 fj1 = new ForkJoin1(num*num);
        ForkJoin1 fj2 = new ForkJoin1((int) Math.pow(num, 2));
        fj1.fork();
        return fj1.compute() + fj2.join();
    }
    public static void main (String[] args) {
        ForkJoinPool pool = new ForkJoinPool();
        System.out.println("Result:  " + pool.invoke(new ForkJoin1(3)));
    }    
}

However, when I run it, I get this error: enter image description here

What am I doing wrong?

Please, note that I'm new at Recursivetask in Java.

  • 1
    You are infinitely calling `compute()` because of `fj1.compute() + fj2.join();` - you never change `num` in your recursion so that first statement will recurse forever. – Elliott Frisch Nov 02 '19 at 16:41
  • 1
    @ElliottFrisch Code doesn't actually "recurse" in the normal sense, since it calls `compute()` on a *different* object, where the value of `num` *is* different (squared, aka `num*num`). It's just that `num*num` repeated recursively forever never becomes `0`, so it'll never end. – Andreas Nov 02 '19 at 16:45
  • Oops, I understand. Does that mean isn't there a way to do ```(3*3)^2.``` using Recursivetask? –  Nov 02 '19 at 16:52
  • 1
    I don't know why you'd need recursion to calculate `(3*3)^2`. Recursion means you're doing the same thing over and over, using the output from one call to seed the input of the next call. BTW: `(3*3)^2 = (3^2)^2 = 3^4 = (3*3)*(3*3) = 3*3*3*3`, or however else you might write that. Guess you could do recursion to calculate `(3^2)^2`, since the square operation is applied recursively. – Andreas Nov 02 '19 at 16:55
  • @Andreas I can't use another way because it's some homework from my school and I have been asked to do it through Recursivetask... –  Nov 02 '19 at 16:57
  • Well, I guess I'll ask to my teacher when I'll see him again. I don't see why he would make us calculate something that's not possible using Recursion... Thanks anyway! –  Nov 02 '19 at 17:09

1 Answers1

0

Your code calls compute with num = 3,
so you create a new object and call compute with num = 9 (3 * 3),
so you create a new object and call compute with num = 81 (9 * 9),
so you create a new object and call compute with num = 6561 (81 * 81),
so you create a new object and call compute with num = 43046721 (6561 * 6561),
so you create a new object and call compute with num = -501334399 (43046721 * 43046721),
...

Oops, numeric overflow. Well it continues anyway, with num having the following values, from the beginning:

3
9
81
6561
43046721
-501334399
2038349057
-1970898431
120648705
1995565057
-1876701183
-1454923775
1989099521
2099150849
977076225
1954152449
-386662399
-773324799
-1546649599
1201668097
-1891631103
511705089
1023410177
2046820353
-201326591
-402653183
-805306367
-1610612735
1073741825
-2147483647
1
1
1
1
1
...

As you can see, num never becomes 0, so the calls never stop.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Oops, I understand. Does that mean isn't there a way to do (3*3)^2. using Recursivetask? –  Nov 02 '19 at 16:52