【问题标题】:Create array in main and fill it with data from seperate threads在 main 中创建数组并用来自不同线程的数据填充它
【发布时间】:2012-10-16 17:42:27
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: java arrays multithreading


    【解决方案1】:

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

    你应该改用myThreadClass(i)。

    【讨论】:

    • 谢谢哥们。原来我所要做的就是t = new Thread( new myThreadClass(i)) 我知道这很简单。再次感谢
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      myClass 中的这一行:

      w = new int[5];
      

      将 w 的所有元素初始化为 0。

      所以,当你打电话时

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

      你正在有效地这样做:

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

      所以 w[] 中唯一会改变的元素是第一个元素。

      【讨论】:

      • 是的,我现在包含修复程序。感谢您的输入原来我只需通过T = new Thread(new myThreadClass(i)); 传递int i = 0;
      • 哇。 不 知道@Jay 的单词撕裂。我已经从示例代码中验证了它。我不知道 JVM 是如何实现这一点的。感谢您的教育。
      【解决方案4】:

      这是针对您的问题的过度设计的解决方案。不用封装也可以,但我决定使用它,因为它使示例更具可读性。

      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);
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-11
        • 2021-04-23
        • 2021-09-03
        • 2018-12-28
        • 1970-01-01
        • 2012-06-23
        • 2021-11-22
        • 1970-01-01
        相关资源
        最近更新 更多