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