/*
 * Main.java
 *
 * Created on 28 de Agosto de 2006, 10:22
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package usathreads;

/**
 *
 * @author root
 */
import java.math.*;
public class Main {
    
    static final int MAX_X=1000;
    static final int MAX_Y=1000;
    static final int MAX_X2=MAX_X/2;
    static final int MAX_Y2=MAX_Y/2;
    
    static final int MAX_E=200;
    
    static int Sorteia(int Max){
        return (int)(Math.random()*Max);
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        /** Gera a Matriz MAX_X por MAX_Y */
        int Matriz[][]=new int[MAX_X][MAX_Y];
        
        /** Coloca no maximo MAX_E valores, podendo ter menos (repetir mesma posição) */
        for(int i=0; i<MAX_E; i++){
            int X=Sorteia(MAX_X);
            int Y=Sorteia(MAX_Y);
            Matriz[X][Y]=1;
        }
        
        ThreadPrincipal thre1=new ThreadPrincipal(MAX_X2, 0, MAX_X2, MAX_Y2, Matriz); // quadrante 1
        ThreadPrincipal thre3=new ThreadPrincipal(0, 0, MAX_X2, MAX_Y2, Matriz); // quadrante 2
        ThreadPrincipal thre2=new ThreadPrincipal(0, MAX_Y2, MAX_X2, MAX_Y2, Matriz); // quadrante 3
        ThreadPrincipal thre4=new ThreadPrincipal(MAX_X2, MAX_Y2, MAX_X2, MAX_Y2, Matriz); // quadrante 4
        
        thre1.start();
        thre2.start();
        thre3.start();
        thre4.start();
        
        while(!(thre1.getPronto()&&thre2.getPronto()&&thre3.getPronto()&&thre4.getPronto())){ // busy waiting
            System.out.print("\n Aguardando...  ");
            System.out.print("Processo 1:"+(thre1.getPronto()?"Pronto ":"Executando "));
            System.out.print("Processo 2:"+(thre2.getPronto()?"Pronto ":"Executando "));
            System.out.print("Processo 3:"+(thre3.getPronto()?"Pronto ":"Executando "));
            System.out.print("Processo 4:"+(thre4.getPronto()?"Pronto ":"Executando "));
        }
        
        System.out.println("\n\nO total é: "+(thre1.getValor()+thre2.getValor()+thre3.getValor()+thre4.getValor()));
        
        System.exit(0);
    }
}
