2014-09-30 71 views
0

我有一个线程,在Java中,把前100个斐波纳契数字放在一个数组中。我如何从线程获取数字。是否有中断,处理,异常,实现,扩展?我一直在添加东西,试验和错误并没有让我在任何地方去理解。我怎样才能从线程返回数组值

import java.util.Scanner; 
import java.io.*; 
import java.lang.Thread;  //don't know if this is needed 

public class FibThread extends Thread{ 

    public FibThread(){ 
     super();     
} 
public void run(int inputNum){ 
    System.out.println(inputNum); 
    long[] fibArray = new long[inputNum]; 
    fibArray[0]=0; 
    fibArray[1]=1; 
    fibArray[2]=1; 
     for(int i = 3; i<inputNum; i++){ 
      fibArray[i]= fibArray[i-1] + fibArray[i-2]; 
     // } 
      //System.out.println(); 
     // for(int j = 0; j<= inputNum; j++){ 
      int output = (int) fibArray[i]; 
      System.out.println(output); 

     } 

} 

public static void main(String[] args){ 
     Scanner keyboard = new Scanner(System.in); 

     FibThread threadOne; 
     int inputNum, itter, output; 
     System.out.println("Please enter the number of Fibonacci numbers to be generated: "); 
     itter = keyboard.nextInt(); 

     //inputNum = itter; 


     threadOne = new FibThread(); 

     threadOne.start(); 


     // for(int j = 0; j<= inputNum; j++){ 
     // int output = (int) fibArray[j]; 
     // System.out.println(output); 

} 


} 
+1

有没有听说过线程中的并发? – 2014-09-30 07:25:18

+0

@VincentBeltman我不明白评论。基本上,你应该有你的fibArray作为你的线程类的成员。当线程完成它的执行(你需要等待),查询它的fibArray值。 – 2014-09-30 07:55:50

+0

没关系。我把这个问题弄错了。您可以将参数传递到FibThread的构造函数中。或者你可以在这个文件中使用一个静态变量来放入所有元素。 – 2014-09-30 08:00:05

回答

1

如果你有一个返回值的“任务”,使之成为Callable

如果您希望可调用函数在后台线程中运行,那么不是自己处理线程的创建和执行,而是通过ExecutorService来抽象它。调用者可以通过传递Callable来与服务交互,并获取将在计算完成时填充值的Future

要修改你的榜样,重命名FibThreadFibCalc

public class FibCalc implements Callable<Integer> { 
    // We need some way to pass in the initial input - must be through the 
    // constructor and we'll store it here 
    private final inputNum; 

    public FibCalc(int inputNum) { 
     this.inputNum = inputNum; 
    } 

    public int call() { 
     // The same as your run() method from before, except at the end: 
     ... 
     return output; 
    } 
} 

// And now for your main() method 
public static void main(String[] args) throws Exception { 
    // As before up to: 
    ... 
    itter = keyboard.nextInt(); 

    // Create a very simple executor that just runs everything in a single separate thread 
    ExecutorService exec = Executors.newSingleThreadExecutor(); 

    // Create the calculation to be run (passing the input through the constructor) 
    FibCalc calc = new FibCalc(itter); 

    // Send this to the executor service, which will start running it in a background thread 
    // while giving us back the Future that will hold the result 
    Future<Integer> fibResult = exec.submit(fibCalc); 

    // Get the result - this will block until it's available 
    int result = fibResult.get(); 

    // Now we can do whatever we want with the result 
    System.out.println("We got: " + result); 
} 

如果你绝对必须创建自己Thread对象(由于一门功课问题的人为限制,或类似的东西 - 我不能看看为什么现实中会这样做),那么这种方法必须有所不同。由于接口的原因run()必须返回void,因此无法返回值。因此,我的方法是将结果存储在FibThread类的局部变量中,然后向该类中添加一个方法(例如public int getResult()),该方法返回该变量。 (如果你这样做,记住你必须自己处理并发问题(即让调用者知道结果已经准备就绪)。一种天真的方法,主方法开始然后立即调用getResult(),这意味着在计算完成之前它几乎肯定会得到一个“空”的结果。对这个问题的粗略解决方案是在产生的线程上调用join(),等待它在访问之前完成结果)。

+0

这是一项家庭作业协议。我正在考虑走很长的路,并做一个同步队列。我试图将数组粘贴到并发问题中,然后以这种方式运行,似乎数组是它的难题。 – Mes 2014-09-30 10:03:34

+0

@Mes返回一个数组不应该比返回任何其他更困难。例如,将我的'FibCalc'示例更改为'Callable ',并且实现的最后一行是'return fibArray;'。如果你可以成功返回一个int,你应该可以返回任何其他对象。 – 2014-09-30 10:37:05