import java.util.concurrent.CountDownLatch; public class CountDown { public static void main(String[] args) { CountDownLatch cd = new CountDownLatch(2); Thread veggie = new Thread(new ChopVegetables(cd)); Thread meat = new Thread(new CookMeat(cd)); Thread tortilla = new Thread(new HeatTortillas(cd)); veggie.start(); meat.start(); tortilla.start(); } } class ChopVegetables implements Runnable { private final CountDownLatch cd; ChopVegetables(CountDownLatch cd) { this.cd = cd; } public void run() { try { String[] vegetables = {"Tomato", "Lettuce", "Onion"}; for(String veggie : vegetables) { System.out.println("Chopping " + veggie); Thread.sleep((int) (Math.random() * 2500)); } System.out.println("All veggies chopped!"); cd.countDown(); } catch(InterruptedException e) { System.exit(1); } } } class CookMeat implements Runnable { private final CountDownLatch cd; CookMeat(CountDownLatch cd) { this.cd = cd; } public void run() { try { System.out.println("Cooking meat"); Thread.sleep((int) Math.random() * 5000); System.out.println("Meat cooked!"); cd.countDown(); } catch(InterruptedException e) { System.exit(1); } } } class HeatTortillas implements Runnable { private final CountDownLatch cd; HeatTortillas(CountDownLatch cd) { this.cd = cd; } public void run() { try { cd.await(); System.out.println("Heating tortillas"); Thread.sleep((int) Math.random() * 3000); System.out.println("Tortillas heated!"); } catch(InterruptedException e) { System.exit(1); } } }