import java.util.concurrent.CountDownLatch; public class CookMeatTask implements Runnable { private final CountDownLatch latch; public CookMeatTask(CountDownLatch latch) { this.latch = latch; } @Override public void run() { try { System.out.println("Cooking meat"); Thread.sleep((int) (Math.random() * 5000)); System.out.println("Meat cooked!"); latch.countDown(); } catch (InterruptedException e) { System.err.println("Meat cooking was interrupted. Dinner is cancelled!"); System.exit(1); } } }