title: Java multithreading - initialize array and sum
body: <p>I have a problem that I'm working on where I need to:</p>
  
  <ul>
  <li>Initialize an array of any given size, where each value of the array will be assigned by 1 thread. So there are as many threads as the length of the array. This has to be done using a static field and cannot be done through a thread constructor</li>
  <li>Sequentially sum the array (from 0 to n) using each each thread, cannot make thread wait if it isn't the next thread to sum, must use a wait/notify.</li>
  </ul>
  
  <p>Could anyone provide some general direction how I could accomplish this? Right now there are problems with synchronization and I'm not sure how to solve them, sometimes values in the array are input as 0 or the summing isn't correct.</p>
  
  <pre><code>public class Processor {
  
      static volatile int numberOfValues = 17;
      static double processedArray[];
      static Thread threadsArray[];
      static volatile int sum;
      static Object lock1 = new Object();
      static Object lock2 = new Object();
  </code></pre>
  
  <p>Adding values to the array. Sometimes zeroes are added instead of the next number from numberOfValues</p>
  
  <pre><code>    private static void initializeArray() {
          threadsArray = new Thread[numberOfValues];
          processedArray = new double[numberOfValues];
  
          for (int i = 0; i &lt; threadsArray.length; i++) {
              threadsArray[i] = new Thread(new Runnable() {
                  public void run() {
                      synchronize(lock1) {
                          processedArray[numberOfValues - 1] = numberOfValues;
                          numberOfValues--;
                      }
  
                  }   
              });
              threadsArray[i].start();
          }
  
          for (int i = 0; i &lt; threadsArray.length; i++) {
              try {
                  threadsArray[i].join();
              } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
  </code></pre>
  
  <p>Summing the values in the array, doesn't always sum to be correct (in this case where the sum is all numbers from 1-17)</p>
  
  <pre><code>        for (int i = 0; i &lt; threadsArray.length; i++) {
              threadsArray[i] = new Thread(new Runnable() {
                  public void run() {
                      synchronize(lock2) {
                          sum += processedArray[numberOfValues];
                          numberOfValues++;
                      }       
  
                  }   
              });
              threadsArray[i].start();
          }
  
          for (int i = 0; i &lt; threadsArray.length; i++) {
              try {
                  threadsArray[i].join();
              } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
  
      }
  </code></pre>
  
  <p>Main</p>
  
  <pre><code>    public static void main(String args[]) {
          initializeArray();
          for (int i = 0; i &lt; threadsArray.length; i++) {
              System.out.println(processedArray[i]);
          }
  
          System.out.println("Sum: " + sum);
      }
  }
  </code></pre>