Im having a problem with Thread that I couldn't understand.
public class Hurry{
public static void main(String[] args) {
Lazy lazy = new Lazy();
lazy.start();
int contadorSegundos=0;
while(lazy.isAlive()){
if(contadorSegundos==5){
lazy.interrupt();
System.out.println(Thread.currentThread().getName()+" ¡¡Yo me voy!!");
break;
}
System.out.println(Thread.currentThread().getName()+" ¿Te queda mucho?");
try {
Thread.sleep(1000);
contadorSegundos++;
}catch(InterruptedException e){}
}//En clase no hemos conseguido la concurrencia, me has dicho que te ponga este comentario.
if(contadorSegundos<5){
System.out.println(Thread.currentThread().getName()+" ¡¡Por fin!");
}
}
}
public class Lazy extends Thread{
public void run() {
int aleatorio = ((int)(Math.random()*(9-2)+2));
for(int i = 0; i<aleatorio;i++){
switch ((int)(Math.random()*(4-1)+1)){
case 1:
System.out.println(Thread.currentThread().getName()+" Me estoy vistiendo");
break;
case 2:
System.out.println(Thread.currentThread().getName()+" Me queda un segundo");
break;
case 3:
System.out.println(Thread.currentThread().getName()+" Esto no me queda bien");
}
try {
Thread.sleep(1000);
}catch (InterruptedException e){}
boolean xd = Thread.currentThread().isInterrupted();
if(Thread.currentThread().isInterrupted()){
System.out.println(Thread.currentThread().getName()+" ¡¡Que cansino eres!");
break;
}
}
if(!Thread.currentThread().isInterrupted()){
System.out.println(Thread.currentThread().getName()+" ¡¡Ya estoy!");
}
}
}
Im practicing concurrency, and when the main executes lazy.interrupt(), the Thread lazy don't detects the interruption with Thread.currentThread().isInterrupted().
The code is supposed to detect when the main interrupts him, but I don't know why it doesn't work.
Any idea? Many thanks!