I'm testing threads in Java and I've stayed in a part where I do not know how to continue. The problem is this:
I have 3 threads running and I would like to know how I can stop the second thread only if the first thread is suspended. The part of suspending the first thread works for me (when it reaches 15 the first thread stops), but I can not make the second one stop. I have seen by some sites that the isAlive method is done, but I do not know how to apply it.
I enclose what I have of the code.
public class Hilos extends Thread {
public Hilos(String nombre) {
super(nombre);
}
@Override
public void run() {
for (int i = 1; i < 26; i++) {
System.out.println("" + getName() + " paso " + i);
if ((i == 15) && (getName().equals("Hilo 1"))) {
System.out.println("" + getName() + " está suspendido.");
suspend();
}
System.out.println("Acaba " + getName());
}
}
public static void main(String[] args) {
Hilos h1 = new Hilos("Hilo 1");
Hilos h2 = new Hilos("Hilo 2");
Hilos h3 = new Hilos("Hilo 3");
h1.start();
h2.start();
h3.start();
}
}