En Scala esto es muy fácil de hacer, y creo que es mejor usar Scala. Aquí hay un ejemplo que extraje de aquí http://danielwestheide.com/ (Guía del Neófito de Scala Parte 16: Dónde ir desde aquí) este tipo tiene un gran blog (no soy ese tipo)
Tomemos un barrista haciendo café.Las tareas a realizar son:
- moler los granos requeridos café (no hay tareas anteriores)
- calor un poco de agua (no hay tareas anteriores)
- Brew un espresso con el café molido y el agua caliente (depende 1 & 2)
- espuma un poco de leche (no hay tareas anteriores)
- combinan la espuma de leche y el café (depende 3,4)
o como un árbol:
Grind _
Coffe \
\
Heat ___\_Brew____
Water \_____Combine
/
Foam ____________/
Milk
en Java utilizando la API de concurrencia esto sería:
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class Barrista {
static class HeatWater implements Callable<String> {
@Override
public String call() throws Exception {
System.out.println("Heating Water");
Thread.sleep(1000);
return "hot water";
}
}
static class GrindBeans implements Callable<String> {
@Override
public String call() throws Exception {
System.out.println("Grinding Beans");
Thread.sleep(2000);
return "grinded beans";
}
}
static class Brew implements Callable<String> {
final Future<String> grindedBeans;
final Future<String> hotWater;
public Brew(Future<String> grindedBeans, Future<String> hotWater) {
this.grindedBeans = grindedBeans;
this.hotWater = hotWater;
}
@Override
public String call() throws Exception
{
System.out.println("brewing coffee with " + grindedBeans.get()
+ " and " + hotWater.get());
Thread.sleep(1000);
return "brewed coffee";
}
}
static class FrothMilk implements Callable<String> {
@Override
public String call() throws Exception {
Thread.sleep(1000);
return "some milk";
}
}
static class Combine implements Callable<String> {
public Combine(Future<String> frothedMilk, Future<String> brewedCoffee) {
super();
this.frothedMilk = frothedMilk;
this.brewedCoffee = brewedCoffee;
}
final Future<String> frothedMilk;
final Future<String> brewedCoffee;
@Override
public String call() throws Exception {
Thread.sleep(1000);
System.out.println("Combining " + frothedMilk.get() + " "
+ brewedCoffee.get());
return "Final Coffee";
}
}
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(2);
FutureTask<String> heatWaterFuture = new FutureTask<String>(new HeatWater());
FutureTask<String> grindBeans = new FutureTask<String>(new GrindBeans());
FutureTask<String> brewCoffee = new FutureTask<String>(new Brew(grindBeans, heatWaterFuture));
FutureTask<String> frothMilk = new FutureTask<String>(new FrothMilk());
FutureTask<String> combineCoffee = new FutureTask<String>(new Combine(frothMilk, brewCoffee));
executor.execute(heatWaterFuture);
executor.execute(grindBeans);
executor.execute(brewCoffee);
executor.execute(frothMilk);
executor.execute(combineCoffee);
try {
/**
* Warning this code is blocking !!!!!!!
*/
System.out.println(combineCoffee.get(20, TimeUnit.SECONDS));
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
System.out.println("20 SECONDS FOR A COFFEE !!!! I am [email protected]#! leaving!!");
e.printStackTrace();
} finally{
executor.shutdown();
}
}
}
Asegúrese de que agrega los tiempos de espera, aunque para asegurarse de que su código no esperará para siempre en algo complete, eso se hace usando Future.get (long, TimeUnit) y luego maneja la falla en consecuencia.
Es mucho mejor en Scala Sin embargo, aquí es como si fuera en el blog: El código para preparar un café sería algo como esto:
def prepareCappuccino(): Try[Cappuccino] = for {
ground <- Try(grind("arabica beans"))
water <- Try(heatWater(Water(25)))
espresso <- Try(brew(ground, water))
foam <- Try(frothMilk("milk"))
} yield combine(espresso, foam)
donde todos los métodos devuelven un futuro (mecanografiado futuro), por ejemplo molienda sería algo como esto:
def grind(beans: CoffeeBeans): Future[GroundCoffee] = Future {
// grinding function contents
}
para todas las implementaciones echa un vistazo al blog, pero eso es todo lo que hay que hacer. También puede integrar Scala y Java fácilmente. Realmente recomiendo hacer este tipo de cosas en Scala en lugar de Java. Scala requiere mucho menos código, mucho más limpio y controlado por eventos.
¿Ha considerado que la Tarea A notifique la Tarea B cuando esté completa? Antes de comenzar la Tarea A, crea una instancia de la Tarea B y agrégala como observador a la Tarea A (Ver Patrón del Observador). –
Guava ['ListenableFuture'] (http://code.google.com/p/guava-libraries/wiki/ListenableFutureExplained) es un poco más amigable acerca de estas cosas que los futuros simples. –