Wednesday, 17 September 2014

CountDownLatch

CountDownLatch is a synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes. A CountDownLatch is initialized with a given count. You can decrement the count value by using countDown(). The await method block until the current count reaches zero. As soon as count reaches zero, Thread awaiting starts running. One of the disadvantage of CountDownLatch is that its not reusable once count reaches to zero you can not use CountDownLatch any more.

For Example,lets take a scenario, where there are 'n' producer threads and 1 consumer thread. Consumer thread must wait until all the producer threads produce the items.

import java.util.concurrent.CountDownLatch;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Producer implements Runnable{
    CountDownLatch latch = null;
    
    Producer(CountDownLatch latch){
        this.latch = latch;
    }
    
    @Override
    public void run(){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println(Thread.currentThread().getName() +": finished");
        latch.countDown();        
    }
}

import java.util.concurrent.CountDownLatch;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Consumer implements Runnable{
    CountDownLatch latch = null;
    
    Consumer(CountDownLatch latch){
        this.latch = latch;
    }
    
    @Override
    public void run(){
        try {
            latch.await();
        } catch (InterruptedException ex) {
            Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println(Thread.currentThread().getName() +": finished");
    }
}

import java.util.concurrent.CountDownLatch;

public class CountDownLatchEx {
    public static void main(String args[]){
        CountDownLatch latch = new CountDownLatch(5);
        
        /* Five Producers */
        Producer p1 = new Producer(latch);
        Producer p2 = new Producer(latch);
        Producer p3 = new Producer(latch);
        Producer p4 = new Producer(latch);
        Producer p5 = new Producer(latch);
        
        Consumer con = new Consumer(latch);
        
        Thread t1 = new Thread(p1);
        Thread t2 = new Thread(p2);
        Thread t3 = new Thread(p3);
        Thread t4 = new Thread(p4);
        Thread t5 = new Thread(p5);
        
        Thread t6 = new Thread(con);
        
        t1.setName("Producer1");
        t2.setName("Producer2");
        t3.setName("Producer3");
        t4.setName("Producer4");
        t5.setName("Producer5");
        t6.setName("Consumer");
        
        t6.start();
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
    }
}

Output
Producer1: finished
Producer2: finished
Producer5: finished
Producer4: finished
Producer3: finished
Consumer: finished

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment