在java中使用线程计算斐波那契
import java.math.BigInteger;
import java.util.concurrent.*;
public class MultiThreadedFib {
private ExecutorService executorService;
public MultiThreadedFib(final int numberOfThreads) {
executorService = Executors.newFixedThreadPool(numberOfThreads);
}
public BigInteger getFibNumberAtIndex(final int index)
throws InterruptedException, ExecutionException {
Future<BigInteger> indexMinusOne = executorService.submit(
new Callable<BigInteger>() {
public BigInteger call()
throws InterruptedException, ExecutionException {
return getNumber(index - 1);
}
});
Future<BigInteger> indexMinusTwo = executorService.submit(
new Callable<BigInteger>() {
public BigInteger call()
throws InterruptedException, ExecutionException {
return getNumber(index - 2);
}
});
return indexMinusOne.get().add(indexMinusTwo.get());
}
public BigInteger getNumber(final int index)
throws InterruptedException, ExecutionException {
if (index == 0 || index == 1)
return BigInteger.valueOf(index);
return getFibNumberAtIndex(index - 1).add(getFibNumberAtIndex(index - 2));
}
}
我想用java线程计算斐波那契序列,以减少计算时间,但答案是错误的,尽管它似乎是正确的。 另一个问题是在启动第 35 号到顶部的新线程时发生内存不足异常。 请帮我 许多问候...
import java.math.BigInteger;
import java.util.concurrent.*;
public class MultiThreadedFib {
private ExecutorService executorService;
public MultiThreadedFib(final int numberOfThreads) {
executorService = Executors.newFixedThreadPool(numberOfThreads);
}
public BigInteger getFibNumberAtIndex(final int index)
throws InterruptedException, ExecutionException {
Future<BigInteger> indexMinusOne = executorService.submit(
new Callable<BigInteger>() {
public BigInteger call()
throws InterruptedException, ExecutionException {
return getNumber(index - 1);
}
});
Future<BigInteger> indexMinusTwo = executorService.submit(
new Callable<BigInteger>() {
public BigInteger call()
throws InterruptedException, ExecutionException {
return getNumber(index - 2);
}
});
return indexMinusOne.get().add(indexMinusTwo.get());
}
public BigInteger getNumber(final int index)
throws InterruptedException, ExecutionException {
if (index == 0 || index == 1)
return BigInteger.valueOf(index);
return getFibNumberAtIndex(index - 1).add(getFibNumberAtIndex(index - 2));
}
}
im gonig to calculate fibonacci sequence with java threads in order to decrease calculating time but answer is wrong Although its seem to be true.
another problem is out of memory exception that occurred in starting new threads for number 35 to the top.
please help me
many regards...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你说你这样做是为了提高性能。有几个问题:
方式。
这个问题。
只需一个线程、一个简单的循环和两个变量,您将获得性能方面难以超越的东西(不使用 一个封闭形式的解决方案,即)。
You say you're doing this to improve performance. There are several issues:
manner.
this problem.
Just have one thread, a simple loop and two variables, and you'll have something that'll be hard to beat performance-wise (without using a closed-form solution, that is).
请参阅斐波那契数 (Java),记忆递归。这应该会让您了解如何实施快速解决方案。
See Fibonacci numbers (Java), memoized recursion. This should give you an idea on how to implement a fast solution.
您需要限制线程数量。我建议您只使用两个线程:一个用于查找索引 - 1,另一个用于查找索引 - 2。当这些方法中的每一个递归地查找前一个索引 - 1 和索引 - 2 时,请在现有线程中执行此操作。
最后,我相信前两个斐波那契数(根数)是 1 和 1。 2、不是0& 1. 请注意,我将
getNumber
更改为返回index + 1
并递归调用自身,而不是返回到服务。1 + 2 = 3 + 2 = 5 ...
0 + 1 = 1(错误)
该算法的另一个问题是它做了两次大量工作,因为它重新计算值。例如,如果您正在查找
F5 = F4 + F3
线程 1:F4 = F3 + F2
线程 2:F3 = F2 + F1
请注意,您计算了 F3 两次。事实上,您几乎每个数字都计算了两次。
You need to limit the number of threads. I would recommend that you only use two threads: one for finding index - 1, the other for finding index - 2. When each of these methods recursively find the previous index - 1 and index - 2, do this in the existing thread.
Finally, I believe the first two Fibonacci numbers (the root numbers) are 1 & 2, not 0 & 1. Notice that I changed
getNumber
to returnindex + 1
and recursively call itself rather than going back to the service.1 + 2 = 3 + 2 = 5 ...
0 + 1 = 1 (error)
Another problem this alg has is that is does a lot of work twice because it recalculates values. For example if you are looking for the
F5 = F4 + F3
Thread 1: F4 = F3 + F2
Thread 2: F3 = F2 + F1
Notice that you calculate F3 twice. In fact you are calculating just about each number twice.