在java中使用线程计算斐波那契

发布于 2024-12-08 14:30:50 字数 1291 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南风起 2024-12-15 14:30:50

你说你这样做是为了提高性能。有几个问题:

  • 您永远不会通过在此使用线程来减少计算时间
    方式。
  • 如果您关心性能,那么递归不是一个好主意
    这个问题。

只需一个线程、一个简单的循环和两个变量,您将获得性能方面难以超越的东西(不使用 一个封闭形式的解决方案,即)。

You say you're doing this to improve performance. There are several issues:

  • You'll never decrease the computation time by using threads in this
    manner.
  • If you care about performance, recursion is a bad idea for
    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).

薄荷梦 2024-12-15 14:30:50

请参阅斐波那契数 (Java),记忆递归。这应该会让您了解如何实施快速解决方案。

See Fibonacci numbers (Java), memoized recursion. This should give you an idea on how to implement a fast solution.

浮生未歇 2024-12-15 14:30:50

您需要限制线程数量。我建议您只使用两个线程:一个用于查找索引 - 1,另一个用于查找索引 - 2。当这些方法中的每一个递归地查找前一个索引 - 1 和索引 - 2 时,请在现有线程中执行此操作。

public BigInteger getNumber(final int index) 
    throws InterruptedException, ExecutionException {
   if (index == 0 || index == 1)
     return BigInteger.valueOf(index + 1);

   return getNumber(index - 1).add(getNumber(index - 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.

public BigInteger getNumber(final int index) 
    throws InterruptedException, ExecutionException {
   if (index == 0 || index == 1)
     return BigInteger.valueOf(index + 1);

   return getNumber(index - 1).add(getNumber(index - 2));
}

Finally, I believe the first two Fibonacci numbers (the root numbers) are 1 & 2, not 0 & 1. Notice that I changed getNumber to return index + 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文