CountDownLatch by example
Java concurrent package provides CountDownLatch class which helps to control execution of set of threads depends on some external activity. CountDownLatch has two main methods countdown() and await(). CountDownLatch’s constructor takes an integer as a parameter which decides its behavior. Calling await() holds execution till countdownLatch’s count(constructor parameter) become zero and countdown() reduces the count on every call. To explain its use, here is an example of horse race. Here every horse is thread and if you have to stimulate race condition , these threads have to start at a time. As well as you should know when all complete the race. We can stimulate this functionality using CountDownLatch. 1: import java.util.*; 2: import java.util.concurrent.*; 3: public class TestCountDownLatch 4: { 5: public static void main(String[] args) throws Exception 6: { 7: List...