我正尝试使用可打电话的器具执行Fibonacci序列,并用3,4,5,6和2000年,推出我的菲博森元的初步价值。 我获得的产出如下:
3 5 8 13 -820905900187520670
问题在于我试图用我可以说的话计算菲比(2000年)。 有些人可以看一下我下面提出的法典,看看我对我的做法有误:
import java.util.concurrent.*;
import java.util.*;
class FibonacciGen implements Callable<Long>{
private Long fib;
public FibonacciGen(long num){
this.fib = num;
}
public Long call(){
return calculateFibonacci(fib);
}
private long calculateFibonacci(long someNum){
long firstNum = 0L;
long secondNum = 1L;
long counter = 0L;
while(counter<someNum){
long fibCalc = secondNum+firstNum;
firstNum = secondNum;
secondNum = fibCalc;
counter= counter+1L;
}
return secondNum;
}
}
public class FibonacciCallable{
public static void main(String[] args){
ExecutorService exec = Executors.newCachedThreadPool();
ArrayList<Callable<Long>> results = new ArrayList<Callable<Long>>();
CompletionService<Long> ecs = new ExecutorCompletionService<Long>(exec);
results.add(new FibonacciGen(3L));
results.add(new FibonacciGen(4L));
results.add(new FibonacciGen(5L));
results.add(new FibonacciGen(6L));
results.add(new FibonacciGen(2000L));
try{
for(Callable<Long> fs:results){
ecs.submit(fs);
}
System.out.println("Submitted all the tasks");
int n = results.size();
for(int i=0;i<n;++i){
System.out.println("Taking the first completed task");
Long r = ecs.take().get();
if(r != null)
System.out.println(r);
}
}
catch(InterruptedException ex){System.out.println(ex);return;}
catch(ExecutionException e){System.out.println(e);}
finally{exec.shutdown();}
}
}
成就