Ahi va una opcion. Seguro que hay formas mas faciles de hacerlo:
public class hilos {
public static void main(String []args) {
// CREAMOS LOS 3 HILOS
Mihilo t1 = new Mihilo(" Hilo 1");
Mihilo t2 = new Mihilo(" Hilo 2");
Mihilo t3 = new Mihilo(" Hilo 3", t1, t2);
// LOS ARRANCAMOS. EL HILO 3 DEBE ESPERAR A LOS OTROS
t3.start();
t2.start();
t1.start();
}
}
class Mihilo extends Thread {
private Mihilo h1;
private Mihilo h2;
public Mihilo(String nombre) {
super(nombre);
}
// PASAMOS AL 3ER HILO LAS REFERENCIAS DE LOS OTROS DOS
public Mihilo(String nombre, Mihilo h1, Mihilo h2) {
this(nombre);
this.h1 = h1;
this.h2 = h2;
}
public synchronized void run() {
// EL 3ER HILO ES EL UNICO QUE TIENE LAS REFERENCIAS h1 Y h2 DISTINTAS DE NULL
// LO UNICO QUE HAY QUE HACER ES ESPERAR A QUE h1 Y h2 ACABEN CON join()
if ((h1 != null) && (h2 != null)) {
try {
h1.join();
h2.join();
} catch (InterruptedException e) {
System.out.println("EXCEPCION: " + e.getMessage());
}
}
for (int i = 1; i <= 10; i++)
System.out.println(this + ": " + i);
}
}