在Java中使用线程和递归来计算Fibonacci数 [英] Using threads and recursion in Java to calculate Fibonacci numbers

查看:247
本文介绍了在Java中使用线程和递归来计算Fibonacci数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java世界中比较新,我遇到了一个我不明白的问题。



我有一个类(获取斐波纳契行) :

  class Fib {
public static int f(int x){
if(x< 2)
返回1;
else
返回f(x-1)+ f(x-2);
}
}

现在的任务是启动f(x-1) )和f(x-2)各自在一个单独的线程中。
一次实现Thread类,另一次实现Runnable。
你可能知道,这是我教授的一个练习。



我知道如何在Java中启动一个Thread我知道整个Thread事物在理论上是如何工作的,但我找不到在这个递归函数中启动单独Threads的解决方案。



在run函数中需要做什么?



可能

  public void run(){
// int foo = start f (this.x-1)
// int bar = start f(this.x-2)
//返回foo + bar?
}

如何在我的runnable函数中粘贴x?
x是否在创建时传入对象?

  Class Fib ... {
int x;
public ... run ...
public ... f(x)....

}


 (新的Fib(x) )。开始(); 

或者我走错了路?

解决方案

为了实现这个目的,你需要1)一种方法将数字传递给新线程,2)启动线程,3)等待线程完成, 4)从线程中获取结果的方法。



您可以通过构造函数传递数字。您可以拥有一个名为answer的公共数据成员来包含计算结果。可以使用 start()方法启动线程, join()方法等待线程到完成。



以下示例演示了这一点。这应该是一个很好的起点;从这里你可以抽象出一些混乱,以获得更好的API。

 公共类Fib扩展线程
{
private int x;
public int answer;

public Fib(int x){
this.x = x;
}

public void run(){
if(x< = 2)
answer = 1;
else {
try {
Fib f1 = new Fib(x-1);
Fib f2 = new Fib(x-2);
f1.start();
f2.start();
f1.join();
f2.join();
answer = f1.answer + f2.answer;
}
catch(InterruptedException ex){}
}
}

public static void main(String [] args)
throws Exception
{
try {
Fib f = new Fib(Integer.parseInt(args [0]));
f.start();
f.join();
System.out.println(f.answer);
}
catch(例外e){
System.out.println(用法:java Fib NUMBER);
}
}
}


I'm relatively new in the Java world and I have a problem which I don't understand.

I have a Class (to get the fibonacci row):

class Fib {
    public static int f(int x){
        if ( x < 2 )
            return 1;       
        else 
            return f(x-1)+ f(x-2);      
    }
}

The task now is to start f(x-1) and f(x-2) each in a separate Thread. One time with implementing the Thread class and the other with implementing Runnable. As you probably know, it's an exercise from my prof.

I know how to start a Thread in Java and I know how this whole Thread thing theoretically works, but I can't find a solution for starting separate Threads in this recursive function.

What has to be done in the run function?

Probably

public void run(){
//int foo=start f(this.x-1)
    //int bar=start f(this.x-2)  
    //return foo+bar?
}

And how can I paste x in my runnable function? Is x passed into the object at creation?

Class Fib ...{
  int x;
  public ... run ... 
  public ... f(x)....

}

in the main method

(new Fib(x)).start();

Or am I on a totally wrong path?

解决方案

For this to work, you need 1) a way to pass the number into the new thread, 2) to start the thread, 3) to wait for the thread to finish, and 4) a way to get the result back from the thread.

You can pass in the number through the constructor. You can have a public data member called "answer" to contain the result of the computation. Starting the thread can be done with the start() method, and the join() method waits for the thread to complete.

The following example demonstrates this. That should be a good starting point; from here you can abstract away some of the messiness to get a better API as desired.

public class Fib extends Thread
{
    private int x;
    public int answer;

    public Fib(int x) {
        this.x = x;
    }

    public void run() {
        if( x <= 2 )
            answer = 1;
        else {
            try {
                Fib f1 = new Fib(x-1);
                Fib f2 = new Fib(x-2);
                f1.start();
                f2.start();
                f1.join();
                f2.join();
                answer = f1.answer + f2.answer;
            }
            catch(InterruptedException ex) { }
        }
    }

    public static void main(String[] args)
        throws Exception
    {
        try {
            Fib f = new Fib( Integer.parseInt(args[0]) );
            f.start();
            f.join();
            System.out.println(f.answer);
        }
        catch(Exception e) {
            System.out.println("usage: java Fib NUMBER");
        }
    }
}

这篇关于在Java中使用线程和递归来计算Fibonacci数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆