/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package borrar;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author felix
 */
public class HiloSimple extends Thread{
    private int cuentaAtras = 10;
    private static int conteoHilos = 0;
    private int numeroHilos = ++conteoHilos;
    Random r;
    
    public HiloSimple() {
        super(Integer.toString(conteoHilos));
        System.out.println("Creando Hilo " + (numeroHilos -1));
        r = new Random();
    }
    
    @Override
    public String toString() {
       return "Hilo # " + getName() + " {" +cuentaAtras+ "}"; 
    }
    
    @Override
    public void run() {
        while(true) {
            System.out.println(this);
            if(--cuentaAtras == 0)
                return;
            
            try {
                sleep(Math.abs(r.nextInt())%500);
            } catch (InterruptedException ex) {
                Logger.getLogger(HiloSimple.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    public static void main(String args[]) {
        for(int i = 0; i< 5; i++)
            new HiloSimple().start();
        
        System.out.println("Todos los Hilos Arrancados");
    } 

}
