[英]Error when trying to calculate (3*3)^2 using Recursive Task (Fork and Join) in Java
I have to use Java Recursive Task (Fork and Join)
to calculate something like this: (3*3)^2.我必须使用Java Recursive Task (Fork and Join)
来计算如下内容:(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:但是,当我运行它时,我收到此错误:
What am I doing wrong?我究竟做错了什么?
Please, note that I'm new at Recursivetask in Java.请注意,我是 Java 中 Recursivetask 的新手。
Your code calls compute
with num = 3
,您的代码使用num = 3
调用compute
,
so you create a new object and call compute
with num = 9 (3 * 3)
,所以你创建一个新的 object 并使用num = 9 (3 * 3)
调用compute
,
so you create a new object and call compute
with num = 81 (9 * 9)
,所以你创建一个新的 object 并使用num = 81 (9 * 9)
调用compute
,
so you create a new object and call compute
with num = 6561 (81 * 81)
,所以你创建一个新的 object 并使用num = 6561 (81 * 81)
调用compute
,
so you create a new object and call compute
with num = 43046721 (6561 * 6561)
,所以你创建一个新的 object 并使用num = 43046721 (6561 * 6561)
调用compute
,
so you create a new object and call compute
with num = -501334399 (43046721 * 43046721)
,所以你创建一个新的 object 并使用num = -501334399 (43046721 * 43046721)
调用compute
,
... ...
Oops, numeric overflow .糟糕,数字溢出。 Well it continues anyway, with num
having the following values, from the beginning:好吧,无论如何它都会继续,从一开始就num
具有以下值:
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.如您所见, num
永远不会变成0
,因此调用永远不会停止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.