Ir al contenido principal

Corriendo hilos en paralelo

Este corte de código, muestra como crear y disparar un grupo de hilos, en paralelo.
Usa la conveniente sintaxis de "String...", para recibir de 0 a N parametros, implementa, mediante clases anonimas los objetos Runnable y utiliza el método join, para bloquear el hilo actual, hasta que el hilo invocado termine su ejecución, así se asegura que todos los hilos terminen su ejecución antes de salir del método runInParallel.

public static void main(String[] args) {

runInParallelExample();
}

public static void runInParallelExample () {

runInParallel(true,
new Runnable () {
@Override
public void run() {

System.out.println("Happy");
}
},
new Runnable () {
@Override
public void run() {

System.out.println("New");
}
},
new Runnable () {
@Override
public void run() {

System.out.println("Year");
}
},
new Runnable () {
@Override
public void run() {

System.out.println("All");
}
}
);
}

/**
* Ejecuta todos los hilos en paralelo
*
* @param wait
* true si desea que el metodo espere hasta que se ejecuten todos los hilos
* @param runnables
* Conjunto de objetos runnables
*/
public static void runInParallel(boolean wait, Runnable... runnables) {

List threads = null;
Thread thread = null;

try {

// Creo los objetos
threads = new ArrayList();

// Creo la lista con los hilos y los ejecuto
for (Runnable r : runnables) {

thread = new Thread(r);
threads.add(thread);
thread.start();
}

// If desea que el metodo espere
if (wait) {
for (Thread t : threads) {
t.join();
}
}
} catch (Exception e) {

/** Catch or throws the error */
}
} // runInParallel.

Comentarios

Entradas más populares de este blog

Pasos para remover Postgresql 8.3 en MAC OS

Tomado de: http://forums.enterprisedb.com/posts/list/1437.page In Mac OSX: (Assuming Default Locations) Via uninstaller: 1) In the installation directory, there will be a uninstall-postgresql.app file will be there, executing (double clicking) that will uninstall the postgresql installation. Manual Uninstallation: 1) Stop the server sudo /sbin/SystemStarter stop postgresql-8.3 2) Remove menu shortcuts: sudo rm -rf /Applications/PostgreSQL 8.3 3) Remove the ini file sudo rm -rf /etc/postgres-reg.ini 4) Removing Startup Items sudo rm -rf /Library/StartupItems/postgresql-8.3 5) Remove the data and installed files sudo rm -rf /Library/PostgreSQL/8.3 6) Delete the user postgres sudo dscl . delete /users/postgres

Depeche Mode - Costa Rica - 8 de Octubre 2009

Que año de conciertos, primero Jaguares, ahora los Cadillacs y en Octubre Depeche Mode, solo nos falta RadioHead, para ser feliz. Así como lo oyen; desde el sitio principal de Depeche (clickea aquí ), pueden ver la siguiente fecha: Thursday, October 8th, 2009 San Jose, Costa Rica En venta: June 15th, 2009 Demasiado bueno!!!!! La cita será en Octubre, eso si no nos mata la fiebre porcina :( saludos, J

Enrique Bunbury - 3 de Octubre 2009 - Heredia CR

Via periodico la nacion, me entero de este conciertazo: http://www.nacion.com/viva/2009/julio/29/viva2042768.html Ah esperar mas detalles y la venta de la entradas, "Para solo si me perdonas ..." EDIT ..... Via http://www.enriquebunbury.com/hellville.aspx (gracias a andres por su post) venta anticipada: Entradas a la venta a partir del 13 de agosto en: www.costaricaticket.com Por ahi estare anunciando, el 13 si las estan vendiendo!