You are on page 1of 1

public class MatrixMultiplication   Creating the matrix

static int

mat = new int[3]

 3 

static int

mat2 = new int[3]

 3 

static int

result = new int[3]

 3 

public static void main(String

  Creating the object of
args)
  random class

  Filling first matrix with
Random rand = new Random()
  random values

int i = 0

  Filling second matrix with
i < mat.length
  random values

 True   False 

int j = 0 int i = 0

   

j < mat[i].length i < mat2.length   Printing the first matrix

 False   True   True   False 

i++ mat[i][j]=rand.nextInt(10) int j = 0 System.out.println("This is first matrix:")

 
j++ int i = 0

 
j < mat2[i].length

 False   True 

i++ mat2[i][j]=rand.nextInt(10) i < mat.length   Printing the second matrix

 True   False 

j++ int j = 0 System.out.println("\nThis is second matrix:")

 
int i = 0

 
j < mat[i].length

 False   True 

System.out.println() System.out.print(mat[i][j]+" ") i < mat.length   Object of multiply Class

 True   False 

i++ j++ int j = 0   Threads Multiply multiply = new Multiply(3,3)

 
MatrixMultiplier thread1 = new MatrixMultiplier(multiply)

j < mat[i].length MatrixMultiplier thread2 = new MatrixMultiplier(multiply)

 False   True 

System.out.println() System.out.print(mat2[i][j]+" ")   Implementing threads MatrixMultiplier thread3 = new MatrixMultiplier(multiply)

i++ j++ Thread th1 = new Thread(thread1)

Thread th2 = new Thread(thread2)

  Starting threads Thread th3 = new Thread(thread3)

th1.start()

th2.start()

th3.start()

th1.join()

th2.join()

th3.join()

 Exception e 

e.printStackTrace()   Printing the result

System.out.println("\n\nResult:")

int i = 0

  End main
i < result.length   End Class
  Multiply Class

 True   False 

int j = 0 class Multiply extends MatrixMultiplication

 
private int i

j < result[i].length private int j

 False   True 

System.out.println() System.out.print(result[i][j]+" ") private int chance

i++ j++ public Multiply(int i, int j)

this.i=i

this.j=j

  Matrix Multiplication
chance=0
  Function

public synchronized void multiplyMatrix()

int sum=0

int a=0

a=0

a<i

 True   False 

sum=0 chance>=i

 False 

  End multiply class
int b=0 chance++
  Thread Class

 
class MatrixMultiplier implements Runnable

b<j private final Multiply mul

 True   False 

sum=sum+mat[chance][b]mat2[b] result[chance][a]=sum public MatrixMultiplier(Multiply mul)

 a 

b++ a++ this.mul=mul

@Override
public void run()

mul.multiplyMatrix()

You might also like