title: Java synchronize 2 threads in parallel
body: <p>-----------------SOLVED , Thank you for your suggestions ! ------------</p>
  
  <p>I have the following code where I have an array of numbers. I want to create 2 threads who execute in parallel. First thread to print number value and second thread multiply.
  Here is my code</p>
  
  <pre><code>class Synchronize {
  
      private boolean writeable = true;
  
      public synchronized void numbers() {
          {
              while (!writeable) {
                  try {
                      wait();
                  } catch (InterruptedException e) {
                  }
              }
              writeable = false;
              notify();
          }
      }
  
      public synchronized void multiply() {
          while (writeable) {
              try {
                  wait();
              } catch (InterruptedException e) {
              }
          }
          writeable = true;
          notify();
      }
  }
  
  class Numbers
          extends Thread {
  
      private Synchronize s;
      int   numbers = 0;
      int[] array;
  
      Numbers(String name, Synchronize s, int[] array) {
          super(name);
          this.s = s;
          this.array = array;
      }
  
      public void run() {
          try {
              for (int i = 0; i &lt;= array.length - 1; i++) {
                  System.out.print("\nIn " + getName() + " number is " + array[i] + "\t");
                  Thread.sleep(1000);
                  s.numbers();
              }
          } catch (Exception e) {
          }
      }
  }
  
  
  class Multiply
          extends Thread {
  
      private Synchronize s;
      int   multiply = 1;
      int[] array;
  
      Multiply(String name, Synchronize s, int array[]) {
          super(name);
          this.s = s;
          this.array = array;
      }
  
      public void run() {
          try {
              for (int i = 0; i &lt;= array.length - 1; i++) {
                  multiply = multiply * array[i];
                  System.out.print("\nIn " + getName() + " multiply is " + multiply + "\t");
                  Thread.sleep(1000);
                  s.multiply();
              }
          } catch (Exception e) {
          }
      }
  }
  
  
  public class NewThread {
  
      public static void main(String[] args) {
  
          int array[] = {
                  1,
                  4,
                  5,
                  2,
                  7,
                  8,
                  9
          };
  
          Synchronize s = new Synchronize();
          new Numbers("Thread #1 ", s, array).start();
          new Multiply("Thread #2 ", s, array).start();
      }
  }
  </code></pre>
  
  <p>Code output is like:</p>
  
  <pre><code>In Thread #1  number is 1   
  In Thread #2  multiply is 1     
  In Thread #1  number is 4   
  In Thread #2  multiply is 4     
  In Thread #2  multiply is 20    
  In Thread #1  number is 5   
  In Thread #1  number is 2   
  In Thread #2  multiply is 40    
  In Thread #1  number is 7
  In Thread #1  number is 8   
  In Thread #2  multiply is 280   
  In Thread #2  multiply is 2240  
  In Thread #1  number is 9   
  In Thread #2  multiply is 20160 
  </code></pre>
  
  <p>How I want it to be</p>
  
  <pre><code>In Thread #1  number is 1   
  In Thread #2  multiply is 1   
  In Thread #1  number is 4   
  In Thread #2  multiply is 4     
  In Thread #1  number is 5
  In Thread #2  multiply is 20    
  In Thread #1  number is 2   
  In Thread #2  multiply is 40    
  In Thread #1  number is 7   
  In Thread #2  multiply is 280   
  In Thread #1  number is 8   
  In Thread #2  multiply is 2240  
  In Thread #1  number is 9   
  In Thread #2  multiply is 20160 
  </code></pre>
  
  <p>I don't want method with queue... I would like just to modify this code if anyone know how to do.</p>