0

好的,所以我遇到了麻烦,也许我只是想得太久或者很愚蠢,但这是我所拥有的以及我正在尝试做的事情:

更新代码全部修复,不再出现运行问题。

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

已编辑-我现在得到正确的输出-检查代码是否有更改

4

4 回答 4

2

在这一部分myThreadClass(w[i])中,您没有传递索引,而是传递了一个值,该值为零,因为w它是一个由 5 个元素组成的数组,所有元素都使用默认值 0 进行了初始化。

你应该这样做myThreadClass(i)

于 2012-10-16T17:44:21.857 回答
1

This line from myClass:

w = new int[5];

initializes all the elements of w to 0.

so, when you call

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

your are effectively doing this:

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

so the only element of w[] that will ever change is the first one.

于 2012-10-16T17:50:04.370 回答
1

w[]最初都是ZERO。您正在将这些值之一传递给线程构造函数

于 2012-10-16T17:49:31.423 回答
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);
        }
    }
}
于 2012-10-16T18:22:29.050 回答