/*
 * 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.concurrent.Semaphore;
public class Threads implements Runnable
{
	private int tmp[][] ;
    private int m1[][] ;
    private int m2[][];
    private int m3[]; // Matrice resultat
    private int k=0,r,ff,bool;// Numero de la ligne de m1 a multiplier avec les colonnes de m2
   int p=0;
   private Semaphore first;
   private String str;
 
    Threads(String str,int m1[][], int m2[][], int m3[], int tmp[][],int r,Semaphore s1)
    {
        this.m1 = m1;
        this.m2 = m2;
        this.m3 = m3;
        this.tmp = tmp;
        this.r = r;
         first = s1;
        this.str = str;
       }
    
    public void run()
    {
    		
    	try	{ 
    		while(m3[r-1]==0){ 
    	
    			  first.acquire();
    			  
    			  
    			  
    			while(m3[p]==1){ p++; }		  
   // if(m3[p]==1){ p++; 			 	
      if(m3[p]==0){
    	  k=p;
    	  m3[p]=1;
    	 // bool=0;
    	  System.out.println(p+" "+str );
    	  }
    	          
     
    
      first.release();
     Thread.sleep(30);
     
    
   
 
        for ( int i = 0;i < r; i++)
        {	// Somme des produits, nous donne *un* element de tmp
            for (int j = 0; j < r; j++)
            {
                //tmp[k][i]=0;
               tmp[k][i] += m1[k][j] * m2[j][i];
               System.out.print(" "+tmp[k][i]);
            	
            	//System.out.print("saise matrec B="+tmp[1][1]);
            }
        }
      
 }
    		
    		
    		  System.out.print("***************************");	
  
    	}  catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            e.printStackTrace();}

    }
}