import java.util.concurrent.CountDownLatch; public class HeatTortillasTask implements Runnable { private final CountDownLatch latch; public HeatTortillasTask(CountDownLatch latch) { this.latch = latch; } @Override public void run() { try { latch.await(); System.out.println("Heating tortillas"); Thread.sleep((int) (Math.random() * 3000)); System.out.println("Tortillas heated!"); } catch (InterruptedException e) { System.err.println("Tortilla heating was interrupted while waiting for other tasks. Dinner is cancelled!"); System.exit(1); } } }