Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that uses multithreading to implement a 3x3 matrix multipli

ID: 3544679 • Letter: W

Question

Write a Java program that uses multithreading to implement a 3x3 matrix multiplication. The specifications for the program are given as follows:

Assume that the content of input matrices A and B are entered by user from standard input (keyboard). You may assume input matrices are called A and B, and the resulting matrix is called Result.

Suppose that your program implements a multiprocessor scheme. So, create a thread for every processor so that each processor will compute one element of the resulting matrix. In other words,

in each thread calculate one element (i.e., multiply the elements of the ith row from A and jth column from B, and add the results to get the value of Result[i.j]).

Explanation / Answer

import java.util.Scanner;
import java.util.concurrent.*;

public class MatrixMultiplication {

public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int[][] matrix1 = new int[3][3];
int[][] matrix2 = new int[3][3];
int[][] matrix3 = new int[3][3];

System.out.println("Insert values in Matrix1.");
for(int i=0; i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.println("Column "+ (i+1) + " Row " + (j+1) + ": ");
matrix1[i][j] = keyboard.nextInt();
}
}

System.out.println("Insert values in Matrix2.");
for(int i=0; i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.println("Column "+ (i+1) + " Row " + (j+1) + ": ");
matrix2[i][j] = keyboard.nextInt();
}
}

Runnable Multiply(matrix1, matrix2, matrix3, 0, 0);
Runnable two = new Multiply(matrix1, matrix2, matrix3, 0,1);
Runnable three = new Multiply(matrix1, matrix2, matrix3, 0,2);
Runnable four = new Multiply(matrix1, matrix2, matrix3, 1,0);
Runnable five = new Multiply(matrix1, matrix2, matrix3, 1,1);
Runnable six = new Multiply(matrix1, matrix2, matrix3, 1,2);
Runnable seven = new Multiply(matrix1, matrix2, matrix3, 2,0);
Runnable eight = new Multiply(matrix1, matrix2, matrix3, 2,1);
Runnable nine = new Multiply(matrix1, matrix2, matrix3, 2,2);

Thread thread1 = new Thread(one);
Thread thread2 = new Thread(two);
Thread thread3 = new Thread(three);
Thread thread4 = new Thread(four);
Thread thread5 = new Thread(five);
Thread thread6 = new Thread(six);
Thread thread7 = new Thread(seven);
Thread thread8 = new Thread(eight);
Thread thread9 = new Thread(nine);

thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
thread6.start();
thread7.start();
thread8.start();
thread9.start();

System.out.println("Matrix 3: ");
for(int i=0; i<3;i++)
{
System.out.println();
for(int j=0;j<3;j++)
{
System.out.print(Matrix3[i][j] + " ");

}
}

}

class Multiply implements Runnable
{
int temp=0;
int m;
int n;

public Multiply(int[][] matrix1, int[][] matrix2, int[][] matrix3, int i, int j)
{
int[][] matrixA = matrix1;
int[][] matrixB = matrix2;
int[][] matrixC = matrix3;
m = i;
n = j;
}
@Override
public void run()
{
for(int p=0;p<3;p++)
{
temp += matrixA[m][p] * matrixB[p][n];
}
matrixC[m][n] = temp;
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote