import java.util.concurrent.CountDownLatch; public class ChopVegetablesTask implements Runnable { private final CountDownLatch latch; public ChopVegetablesTask(CountDownLatch latch) { this.latch = latch; } @Override 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 vegetables chopped!"); latch.countDown(); } catch (InterruptedException e) { System.err.println("Vegetable chopping was interrupted. Dinner is cancelled!"); System.exit(1); } } }