QUiero para un thread desde otro y si no se puede diganme como hago un delay para
pararlo a x segundos desde fuera del thread , aqui va el code
import java.lang.Thread;
/**
*
* @author hpmini
*/
public class Threads extends Thread{
public static boolean running = true;
public void run(){
while(running){
System.out.println("Saludos");
}}
public static void shtdwn(){
running = false;}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Threads Threads1 = new Threads();
Threads1.start();
stopIt stop = new stopIt();
stop.start();
}
}
class stopIt extends Thread{
public void run(){ try{
Thread.sleep(1000);
Threads.shtdwn();
} catch (InterrumpedException iox){}
}
public boolean running = false;
}
En realidad funciona, aunque no es la mejor manera de hacerlo. Lo único que veo mal es que pones: InterrumpedExeception; la manera correcta es InterruptedException:
public class Threads extends Thread {
public static boolean running = true;
public void run() {
while (running) {
System.out.println("Saludos");
}
System.out.println("Fuck this, I'm out");
}
public static void shtdwn() {
running = false;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Threads Threads1 = new Threads();
Threads1.start();
stopIt stop = new stopIt();
stop.start();
}
}
class stopIt extends Thread {
public void run() {
try {
Thread.sleep(1000);
Threads.shtdwn();
} catch (InterruptedException iox) {
}
}
public boolean running = false;
}
funciono gracias hay alguna otra forma de hacerlo¿?
de parar un thread desde otro thred¿?