Java - multihilos

 
Vista:

multihilos

Publicado por Negro (2 intervenciones) el 08/11/2003 22:19:16
tengo el siguiente problema:
tengo que hacer correr 2 hilos. y una vez que termine alguno de los 2 debo arrancar un tercer hilo.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:multihilos

Publicado por i92jurir (64 intervenciones) el 09/11/2003 14:33:51
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);

}

}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:multihilos

Publicado por Negro (2 intervenciones) el 09/11/2003 20:01:19
miles de gracias, era justo lo que necesitaba
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar