/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package matrice;

/**
 *
 * @author youcef
 */

import java.util.Scanner;
import java.util.concurrent.Semaphore;
public class Matrice
{	
	private static int size,i,j,k;
private    static int matrice1[][]; // Premiere matrice
private   static int matrice2[][];
private   static int resultat[][];// Deuxieme matrice
private   static int vecteur[]; // Matrice resultat
     
    public static void main(String[] args) throws InterruptedException
    {
    	Semaphore semaphore = new Semaphore(1);
        java.util.Random r=new java.util.Random( ) ; 
//int t1=(Integer.toString(r.nextInt(25))); nbr aleatoire de 1 a 25 
       
        Scanner read =new Scanner(System.in);	
		System.out.print("Enter the size of the matrice : ");
	               size =read.nextInt();

        // Generation des matrices
        matrice1 = new int[size][size];
        matrice2 = new int[size][size];
        resultat = new int[size][size];
        vecteur = new int[size];
         
       	System.out.print("enter the elements of the first matrix: ");
        System.out.print("\n");
       	for(i=0;i<size;i++)	{	
       		for(j=0;j<size;j++)	{	
             
                   k=r.nextInt(25)+1; 
       			matrice1[i][j]=k;
                      System.out.print(" "+matrice1[i][j]);
            	if(j==size-1) System.out.print("\n");  
       			   		}
       		}
       	
        for(i=0;i<size;i++){	
       		for(j=0;j<size;j++){	       
       			resultat[i][j]=0;}}
       	
       	System.out.print("enter the elements of the second matrix: ");
        System.out.print("\n");
       	for(i=0;i<size;i++)	{	
       		for(j=0;j<size;j++)	{	
              // 	k=read.nextInt();
                    k=r.nextInt(25)+1;
       			matrice2[i][j]=k;
                        System.out.print(" "+matrice2[i][j]);
            	if(j==size-1) System.out.print("\n");
       			   		}
       		}
       	for(j=0;j<size;j++){	vecteur[j]=0;} 
     
       
        // Creation du groupe de thread
       	
       ThreadGroup threads_Group = new ThreadGroup("Multi_threads");
       
         
        // Un thread pour chaque ligne
       
       
        new Thread(threads_Group, new Threads("thread1",matrice1, matrice2, vecteur,resultat,size,semaphore)).start();
        new Thread(threads_Group, new Threads("thread2",matrice1, matrice2, vecteur,resultat,size,semaphore)).start();
        new Thread(threads_Group, new Threads("thread3",matrice1, matrice2, vecteur,resultat,size,semaphore)).start();

         
             
        
        /************************/
        
        
        Thread.sleep(2000);
       System.out.print("\n");
        for ( int i = 0;i < size; i++)
        {
           
            for (int j = 0; j < size; j++)
            {
            
            	System.out.print(" "+resultat[i][j]);
            	if(j==size-1) System.out.print("\n");
            	
            }
        }
      
}
    }