Ver Mensaje Individual
  #1 (permalink)  
Antiguo 14/04/2008, 13:36
gines97
 
Fecha de Ingreso: abril-2008
Mensajes: 1
Antigüedad: 16 años, 1 mes
Puntos: 0
hilos o thread en java

Hola a todos!
Estoy aprendiendo a a trabajar con hilos o thread en java. El asunto es que comence con un programa simple, pero no entiendo el resultado.
Es decir, creo dos hilos del mismo tipo, pero resulta que cada vez que corro el programa, me da resultados distintos y no entiendo porque.
A continuación les paso el código a ver si alguien me puede ayudar.
Saludos a todos.


//Aqui creo los hilos tipo SINOThread
public class SINOThread extends Thread{
private static String SINO;
static int Contador=0;

public SINOThread (String s){
super();
SINO=s;
}
// public static void main(String[] args) {
public void run(){
//SINOThread a=new SINOThread("YES");
//a.start();
int j;
for (j = 0; j<=20; j++) {
System.out.println(++Contador+" "+SINO+" ");
}
}
}



///Aca es donde instancio la clase anterior e inicio los hilos del tipo SINOThread
public class HilosSiNo {


public static void main(String[] args) {
System.out.println("Iniciando HilosSINO");
String a="SI";
String b ="NO";
SINOThread Y= new SINOThread(a);
SINOThread N=new SINOThread(b);
System.out.println("****************************** ****");
try{
//while (true){
Y.start();
N.start();
// }
}
catch (IllegalThreadStateException e) {
System.out.println("ERORR");
}
finally{
System.out.println("****************************** ****");
Y.stop();
if(Y.isAlive()==true){
System.out.println("El thread Y ha sido detenido");
}
else{
System.out.println("El thread Y esta vivo");
}
System.out.println("****************************** ****");
N.stop();
if(N.isAlive()==true){
System.out.println("El thread N ha sido detenido");
}
else{
System.out.println("El thread N esta vivo");
}
System.out.println("FIN");
System.out.println("****************************** ****");
}
}
}