title: Multithreading matrix multiplication in Java
body: <p>i'm trying to build a program for multiplying two matrices (A[a,b], B[c,d]) using a*d threads (that will be used to print the sum of one index in the finished matrix), for this purpose, i'm using a 'monitor' class that will be used as a controller to synchrosize between the threads, 'multiplier' class to represent the single thread and a main program class. My idea is that the threads will have their calculations, and when thread(0,0) will print his sum, he will signal the next in line. For some reason after printing the first index - all the threads stay in waiting mode and won't test my condition. Could you look at my code and tell me where is my mistake?</p>
  
  <p>Monitor class:</p>
  
  <pre><code>import java.util.concurrent.locks.Condition;
  import java.util.concurrent.locks.Lock;
  import java.util.concurrent.locks.ReentrantLock;
  
  final class Monitor {
      private Lock lock;
      int index;
      Condition cond; 
      public Monitor () {
          lock = new ReentrantLock();
          cond = lock.newCondition();
          this.index = 0;
      }
  
      public synchronized void finished(int x, double sum) throws InterruptedException {
          lock.lock();
          if(index != x) {
              while(index != x) cond.await();
              System.out.printf("%9.2f ",sum);
              index++;
              lock.unlock();
              cond.signalAll();
            }
          else {
              System.out.printf("%9.2f ",sum);
              index++;
              try { lock.unlock(); }
              catch (java.lang.IllegalMonitorStateException e) {};
              try { lock.unlock(); }
              catch (java.lang.IllegalMonitorStateException e) {};
          }
          if(index % 5 == 0) System.out.println();
      }
  }
  </code></pre>
  
  <p>Multiplier:</p>
  
  <pre><code>public class Multiplier extends Thread {
      private int index;
      private double [] vectorOne;
      private double [] vectorTwo;
      private Monitor monitor;
      private double sum;
  
      //constructor
      public Multiplier(int index, Monitor monitor,double [] vectorOne,double [] vectorTwo) {
          this.index = index;
          this.monitor = monitor;
          this.vectorOne = vectorOne;
          this.vectorTwo = vectorTwo;
      }
  
      public void VecMulti() {
          sum = 0;
          for (int i = 0 ; i &lt; vectorOne.length ; i++) 
              sum += vectorOne[i] * vectorTwo[i];
      }
  
      public double getSum() {
          return sum;
      }
  
      public void run() {
          VecMulti();
          try {
              monitor.finished(index, sum);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Main class:</p>
  
  <pre><code>public class MatrixMultiTest {
      public static void main(String[] args) {
          Monitor monitor = new Monitor(3*5);
          Matrix A = Matrix.random(3,4);
          Matrix B = Matrix.random(4,5);
          System.out.println("Matrix No1");
          A.show();
          System.out.println();
          System.out.println("Matrix No2");
          B.show();
          System.out.println();
          System.out.println("Multi Matrix");
  
          for (int i = 0; i &lt; 3; i++)
              for (int j = 0; j &lt; 5; j++) {
                  Multiplier myThr = new Multiplier(i*5+j,
                          monitor,A.getRow(i),B.getCol(j));
                  myThr.start();
                  try {
                      myThr.join();
                  } catch (InterruptedException e) {
                  //  TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
      }
  }
  </code></pre>