0

Okay so I am having trouble with this maybe i have just been thinking too long or am dumb but here is what i have and what i am trying to do:

Update- code all fixed no more run problems.

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

   }
}

in my separate class myThreadClass.java i have the following:

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

}

when i print out the output of w from myClass:

i get w = 1 0 0 0 0

but i want w = 1 1 1 1 1

EDITED- i am now getting the right output - check the code for changes

kandroidj
  • 13,784
  • 5
  • 64
  • 76

4 Answers4

2

In this part myThreadClass(w[i]) you are not passing an index, you are passing a value, which is zero because w is an array of 5 elements, all of them initialized with the default value of 0.

You should do myThreadClass(i) instead.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • Thanks man. Turns out all I had to do is `t = new Thread( new myThreadClass(i))` I knew it was something really simple. Thanks again – kandroidj Oct 16 '12 at 17:50
1

w[] is initially all ZERO. you are passing one of these values to the thread constructor

taufique
  • 2,701
  • 1
  • 26
  • 40
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.

Jay Elston
  • 1,978
  • 1
  • 19
  • 38
  • yepp i am including the fix now. Thanks for your input turns out i just have to pass the `int i = 0;` by `T = new Thread(new myThreadClass(i));` – kandroidj Oct 16 '12 at 17:55
  • Wow. Did _not_ know about word tearing @Jay. I've verified it from example code. I have no idea how the JVM implements this. Thanks for the education. – Gray Oct 16 '12 at 18:42
0

Here's an over-engineered solution for your problem. You could do just fine without encapsulation, but I decided to use it because it makes the example more readable.

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);
        }
    }
}
Sami Korhonen
  • 1,254
  • 8
  • 17