2014-09-30 73 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