Guest User

Matrix

a guest
Apr 23rd, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class Matrix {
  4.   //Creating the matrix
  5.   static int[][] mat = new int[3][3];
  6.   static int[][] mat2 = new int[3][3];
  7.   static int[][] result = new int[3][3];
  8.   
  9.   public static void main(String [] args){
  10.     
  11.     //Object _random class
  12.     Random rand = new Random();
  13.     
  14.     
  15.     //Filling first mtrx
  16.     for (int i = 0; i < mat.length; i++) {
  17.       for (int j = 0; j < mat[i].length; j++) {
  18.         mat[i][j]=rand.nextInt(10);
  19.       }
  20.     }
  21.     
  22.     //Filling second mtrx
  23.     for (int i = 0; i < mat2.length; i++) {
  24.       for (int j = 0; j < mat2[i].length; j++) {
  25.         mat2[i][j]=rand.nextInt(10);
  26.       }
  27.     }
  28.     
  29.     try{
  30.       //Object of multiply Class
  31.       Multiply multiply = new Multiply(3,3);
  32.       
  33.       //Threads
  34.       MatrixMultiplier thread1 = new MatrixMultiplier(multiply);
  35.       MatrixMultiplier thread2 = new MatrixMultiplier(multiply);
  36.       MatrixMultiplier thread3 = new MatrixMultiplier(multiply);
  37.       
  38.       //Implementing threads
  39.       Thread th1 = new Thread(thread1);
  40.       Thread th2 = new Thread(thread2);
  41.       Thread th3 = new Thread(thread3);
  42.       
  43.       //Starting threads
  44.       th1.start();
  45.       th2.start();
  46.       th3.start();
  47.       
  48.       th1.join();
  49.       th2.join();
  50.       th3.join();
  51.       
  52.     }catch (Exception e) {
  53.       e.printStackTrace();
  54.     }
  55.     
  56.     
  57.     System.out.println("\n\nResult:");
  58.     for (int i = 0; i < result.length; i++) {
  59.       for (int j = 0; j < result[i].length; j++) {
  60.         System.out.print(result[i][j]+" ");
  61.       }
  62.       System.out.println();
  63.     }
  64.   }
  65.   
  66. }
  67.  
  68. //Multiply Class
  69. class Multiply extends Matrix {
  70.   
  71.   private int i;
  72.   private int j;
  73.   private int chance;
  74.   
  75.   public Multiply(int i, int j){
  76.     this.i=i;
  77.     this.j=j;
  78.     chance=0;
  79.   }
  80.   
  81.   //Multiplication
  82.   public synchronized void multiplyMatrix(){
  83.     
  84.     int sum=0;
  85.     int a=0;
  86.     for(a=0;a<i;a++){
  87.       sum=0;
  88.       for(int b=0;b<j;b++){
  89.         sum=sum+mat[chance][b]*mat2[b][a];
  90.       }
  91.       result[chance][a]=sum;
  92.     }
  93.     
  94.     if(chance>=i)
  95.     return;
  96.     chance++;
  97.   }
  98. }
  99.  
  100. class MatrixMultiplier implements Runnable {
  101.   
  102.   private final Multiply mul;
  103.   
  104.   public MatrixMultiplier(Multiply mul){
  105.     this.mul=mul;
  106.   }
  107.   
  108.   @Override
  109.   public void run() {
  110.     mul.multiplyMatrix();
  111.   }
  112. }
  113. }
Add Comment
Please, Sign In to add comment