2012-10-16 64 views
0

好吧,所以我遇到了這個問題,也許我只是想太長時間或者很笨,但這裏是我所擁有的以及我想要做的事情:在main中創建數組並填充來自單獨線程的數據

更新代碼全部固定不再有運行問題。

public class myClass program { 
    int [] w = null; 
    int [] x = null; 
    Thread T = null; 
    public static void main(String [] args){ 
    x = new int[5]; 
    w = new int[5]; 

// here i am trying to invoke a new thread passing the index 
// of my array, then incrementing the index each time i create a new thread 
// the purpose is to fill each index each time the new thread runs. 

    for(int i = 0; i < w.length; i ++){ 
     // T = new Thread(new myThreadClass(w[i])); // only passes 0 take this out and 
     T = new Thread(new myThreadClass(i));  // pass i so the position changes 
     T.start(); 
     try{ 
     Thread.sleep(100); 
     }catch(Exception e){} 

    } 
} 
我單獨的類myThreadClass.java

我有以下幾點:

public class myThreadClass extends Thread{ 
int [] w = null; 
int position = 0; 
int value = 1; 

    public myThreadClass(int p){ 
    this.position = p 
    w = myClass.w; 
    } 

    @Override 
    public void run(){ 
    // synchronize the thread so there is no memory cache problems 
    // 
    synchronized(w){ 
     w[position] = value; 
    } 
    } 

} 

當我從myClass的打印出W的輸出:

我得到w = 1 0 0 0 0

但我想要w = 1 1 1 1 1

EDITED-我現在獲得正確的輸出 - 檢查更改

回答

2

在這一部分myThreadClass(w[i])您沒有傳遞一個索引的代碼,要傳遞的值,這是因爲零是w 5個元素的數組,它們全部使用默認值0初始化。

您應該改爲myThreadClass(i)

+0

謝謝你。結果我所要做的就是't = new Thread(new myThreadClass(i))'我知道這非常簡單。再次感謝 –

+0

+1的真正問題丹。 :-) – Gray

1

w[]最初全部是ZERO。你逝去的這些值中的一個線程構造

1

這條線從MyClass的:

w = new int[5]; 

初始化w的爲0。

所以所有的元素,當你調用

T = new Thread(new myThreadClass(w[i])); 

你正在有效地做到這一點:

T = new Thread(new myThreadClass(0)); 

所以w []永遠會改變的唯一元素就是第一個。

+0

yepp我現在包括修復程序。感謝您的輸入,我只需要通過'T = new Thread(new myThreadClass(i))'' –

+0

哇'來傳遞'int i = 0;'。不知道有關撕裂文字@Jay。我已經從示例代碼驗證了它。我不知道JVM如何實現這一點。感謝您的教育。 – Gray

0

以下是針對您的問題的過度設計解決方案。沒有封裝,你可以做得很好,但我決定使用它,因爲它使這個例子更具可讀性。

public class Test { 
    public static void main(String[] args) { 
     // Create the resultset containing the result 
     ResultSet resultSet = new ResultSet(5); 
     Thread[] threads = new Thread[resultSet.getSize()]; 

     // Create threads 
     for (int i = 0; i < resultSet.getSize(); i++) { 
      threads[i] = new Thread(new TestTask(
        resultSet.createResultSetter(i))); 
     } 

     // Start threads 
     for (int i = 0; i < resultSet.getSize(); i++) { 
      threads[i].start(); 
     } 

     // Wait until threads complete 
     for (int i = 0; i < resultSet.getSize(); i++) { 
      try { 
       threads[i].join(); 
      } catch (InterruptedException exception) { 
       // ??! 
      } 
     } 

     // Print the result 
     for (int i = 0; i < resultSet.getSize(); i++) { 
      System.out.println(resultSet.getResult(i)); 
     } 
    } 

    /** 
    * Interface used to set the result 
    */ 
    public static interface ResultSetter { 
     public void setResult(int result); 
    } 

    /** 
    * Container class for results 
    */ 
    public static class ResultSet { 
     private final int[] results; 

     public ResultSet(int size) { 
      results = new int[size]; 
     } 

     public int getSize() { 
      return results.length; 
     } 

     public ResultSetter createResultSetter(final int position) { 
      return new ResultSetter() { 
       public void setResult(int result) { 
        ResultSet.this.setResult(position, result); 
       } 
      }; 
     } 

     public synchronized int getResult(int position) { 
      return results[position]; 
     } 

     public synchronized void setResult(int position, int result) { 
      results[position] = result; 
     } 
    } 

    /** 
    * A task executed by a thread 
    */ 
    public static class TestTask implements Runnable { 
     private ResultSetter resultSetter; 

     public TestTask(ResultSetter resultSetter) { 
      this.resultSetter = resultSetter; 
     } 

     @Override 
     public void run() { 
      resultSetter.setResult(1); 
     } 
    } 
}