Matrix Multiplication (Square of a Matrix) using Threads in JAVA

This program will take a text file as a command line argument. The text file will have the first row indicating the total number of rows and columns which will be equal as this program is programmed to read a square matrix from a file and calculate its square using threads.

Content of the text file to be given as an argument

14
0 1 0 7 5 3 1 0 1 0 7 5 3 1
2 0 0 3 2 2 2 3 0 2 9 0 2 3
2 1 1 2 2 2 4 0 0 2 4 3 3 5
3 0 2 9 0 2 6 1 2 3 4 5 6 7
2 0 0 3 2 2 2 3 0 2 9 0 2 3
2 1 1 2 2 2 4 0 0 2 4 3 3 5
3 0 2 9 0 2 6 1 2 3 4 5 6 7
0 1 0 7 5 3 1 2 0 0 3 2 2 2
3 0 2 9 0 2 3 2 1 1 2 2 2 4
0 0 2 4 3 3 5 3 0 2 9 0 2 6
1 2 3 4 5 6 7 0 1 0 7 5 3 1
2 0 0 3 2 2 2 3 0 2 9 0 2 3
2 1 1 2 2 2 4 0 0 2 4 3 3 5
3 0 2 9 0 2 6 1 2 3 4 5 6 7

The first row indicates the total number of rows and columns, in this case, rows=cloumns=14.
And the next rows will be the actual matrix which we want to multiply with itself. The program will calculate the square of the matrix written in your file.

import java.util.*;
import java.io.*; 

class MMThread extends Thread{  

int row=0,k,j,sum=0;
int first[][],multiply[][];

MMThread(int row,int first[][],int multiply[][]){
this.row = row;
this.first = first;
this.multiply = multiply;


public void run(){  
for (j = 0; j < first[row].length; j++){

           for (k = 0; k < first.length; k++){

              sum = sum + first[row][k]*first[k][j];
           }

           multiply[row][j] = sum;
           sum = 0;
        }

}

public class MM{

   public static void main(String args[])  throws IOException
   {
  int rows=0,cols=0,i=0,j=0,line=0;

  if(args.length==0){
  System.out.println("Matrix File Not Provided");
  return;
  }

  Scanner scanner = new Scanner(new File(args[0]));

  int first[][] = null;

  while(scanner.hasNextInt())
{
if(line==0){
rows = scanner.nextInt();
cols = rows;
first = new int [rows][cols];
line++;
}
    
    first[i][j] = scanner.nextInt();
    if(j==rows-1){
    i++;
    j=0;
    }else{
    j++;
    }
}

  System.out.println("\nOriginal Matrix\n");

  for (i = 0; i < rows; i++){
     for (j = 0; j < cols; j++)
        System.out.print(first[i][j]+"\t");
    System.out.println();
  }

     int second[][] = first;
     int multiply[][] = new int[rows][cols];

     MMThread mMThread[] = new MMThread[rows];

     for(int l=0;l<mMThread.length;l++){
      mMThread[l] = new MMThread(l,first,multiply);
      mMThread[l].start();
     }

     while(Thread.activeCount() > 1);

     System.out.println("\nProduct of the matrices:\n");

     for (i = 0; i < rows; i++){

        for (j = 0; j < cols; j++)
           System.out.print(multiply[i][j]+"\t");

        System.out.print("\n");

     }
   }
}

Output:

square of a matrix using threads in java

Comments

Post a Comment

Popular posts from this blog

Change your Application background in Scene Builder || JavaFX FXML Tutorial

5 Programming Languages To Learn in 2019

c/c++ Programming questions || Practice Problems for Beginners || C Programs List