title: Multiplying matrices by threads in java
body: <p>Currently I am creating a program that multiplies two matrices by using threads.  This is a critical part of my learning of Java that I can't seem to figure out, so I'd really appreciate some assistance.</p>
  <pre><code>package matrix;
  
  import java.util.Random;
  
  
  class ColumnCalculator implements Runnable {
  
      Matrix m1;
      Matrix m2;
      Matrix result;
      int col_idx; //specify which column of the result is going to be calculated
  
      ColumnCalculator(Matrix m_1, Matrix m_2, Matrix r, int col) {
          m1 = m_1;
          m2 = m_2;
          result = r;
          col_idx = col;
      }
  
      @Override
      public void run() {
          // TODO Auto-generated method stub
          //Implementation here...
          //calculating all the elements in the column of index (specified by &quot;col_idx&quot;) of the result matrix
      }
  
  }
  
  public class Matrix {
  
      int rows; // Define the number of rows
      int cols; // Define the number of columns
      double values[][];
  
      Random rand = new Random();
  
      /*First constructor: with row and column as the input that creates a matrix with the specified size and 
       * assign each elements with randomly generated number*/
      Matrix(int r, int c) {
          rows = r;
          cols = c;
          values = new double[r][c];
  
          for (int y = 0; y &lt; rows; y++) {
              for (int x = 0; x &lt; cols; x++) {
                  values[y][x] = rand.nextDouble() * 10.0; //generating a double number between 0 and 10
              }
          }
  
      }
  
      /*First constructor: with row, column, and a 2D array as the input. Similar to the first constructor
       * above, but instead of randomly generating, it assigns the elements with the third argument double 2D array.  */
      Matrix(int r, int c, double v[][]) {
          //Implementation here...
          int x;
          int y;
          rows = r;
          cols = c;
          values = v;
          int row_array = v.length;
          int cols_array = v[0].length;
          values = new double[r][c];
          
          if (row_array &gt;= rows &amp;&amp; cols_array &gt;= cols)
          {
              
          
          for (int temp_y = 0; temp_y &lt; rows; temp_y++) 
          {
              for (int temp_x = 0; temp_x &lt; cols; temp_x++) 
              {
                  values[temp_y][temp_x] = v[temp_y][temp_x];
              }
          }
          } else {
              for (int temp_y = 0; temp_y &lt; rows; temp_y++) {
                  for (int temp_x = 0; temp_x &lt; cols; temp_x++){
                      if (row_array &gt;= temp_y || cols_array &gt;= temp_x){
                        values[temp_y][temp_x] = v[temp_x][temp_y];  
                      } else {
                          values[temp_y][temp_x] = 0;
                      }
                          
                      
                  }
              }
          }
      }
  
      /*Output the matrix to the console*/
      void print() {
          for (int y = 0; y &lt; rows; y++) {
              for (int x = 0; x &lt; cols; x++) {
                  System.out.print(values[y][x] + &quot;, &quot;);
              }
              System.out.println();
          }
      }
  
      /*Matrix product without thread: let the current matrix times the input argument matrix m
       * and return the result matrix
       * Below the multiplication is done and the a dimension check is performed
      Matrix multiplyBy(Matrix m) {
          
         
          
          
  
          Matrix result = new Matrix(rows, m.cols); //Initialize the result matrix
         if(rows != m.cols){
             System.out.println(&quot;Matrices cannot do multiplication!&quot;);
             result = null;
         }else{
          for (int y = 0; y &lt; result.rows; y++) {
              for (int x = 0; x &lt; result.cols; x++) {
                  result.values[y][x] = 0; //the yth row of current matrix x the xth column of m
                  for (int i = 0; i &lt; cols; i++) {
                      result.values[y][x] += values[y][i] * m.values[i][x];
                  }
  
  //              A        *         B         = result
  //             [5, 3            [1, 2, 3        [? ? (y = 0, x = 1) ?
  //              2, 0]            4, 5, 6]        ? ?                ?]
              }
          }
         }
          return result;
         
      }
  
      /*Implementation: instead using loops above to calculate each elements, 
       * here you will use threads to accomplish the matrix product task.
       * Similar to the &quot;multiplyBy()&quot; above, the input matrix m represents
       * the second matrix that you will use the current matrix to times. The
       * returned Matrix will be the product result matrix.  
       * The code below is just an example of what I'm thinking, but it is not complete
       * I need to create multiple threads to do the multiplication with each thread
       * computing one column within the result matrix*/
      Matrix multiplyByThreads(Matrix m) {
          //Implementation here...
  
          Matrix result = null;
  
          Thread thread = new Thread(new ColumnCalculator(this, m, result, 5));
  
          return result;
      }
  
      /* The main function for evaluation purpose*/
      public static void main(String[] args) {
          Matrix m1 = new Matrix(250, 150);
  
          Matrix m2 = new Matrix(150, 200);
  
          System.out.println(&quot;Begin matrix product&quot;);
  
          Matrix result = m1.multiplyBy(m2);
  
          System.out.println(&quot;Ending matrix product&quot;);
  
          //Implementation here...
          //I can test my multiplyByThreads() in both accuracy and time performance
      }
  }
  </code></pre>
  <p>As seen, I've already created multiple functions to both define the matrices and check if they can be multiplied.  However, I am fairly lost when it comes to filling out the multiplybyThreads function.  I'm thinking that it should multiply the second matrix m and return the result matrix by using threads.  Also, I've already defined a run() function in the ColumnCalculator to use.  I'm also thinking that I should allocate 10 threads at a time, then wait for all 10 threads to complete to then initiate the next 10 threads.  I've also found that the thread.join() function may be useful for the waiting scheme. After all of this, like I said, I'm fairly lost so some help creating this function would be amazing.</p>