Wednesday, June 13, 2018

Concurrency CountDownLatch Example in java


In this article, we will learn about the concurrency package CountDownLatch class which is actually used when we want to start the thread after some operation already completed. (ex-start a thread after 2 threads already finished some operation ).


Also Read: Multithreading in java

I will try to explain Count Down Latch using the below problem statement.

#Question

Write a multithreaded program in which there are 10 threads and we need to start the threads and when all threads are started then only we need to stop the threads.


#Solution

The very first thing comes in my mind is CountDownLatch, and why it comes has a valid reason as we have a case where we have to wait for all the threads to start then only we will going to stop the threads.

Why CountDownLatch here?
When we say we need to start 10 threads what we will do is to just run a loop till 10 counter and start initialize each thread and start the thread and then what we will do again we loop the thread for 10 and we start to stop the threads but the problem is we are not sure whether all the 10 threads are already started or not. As it not compulsory that we start the thread when actually JVM start it actually.




So we need something which will assure that yes all the threads are started now you can go ahead to stop all of them.

Now let's see the program implementation and what will be its output.

import java.util.concurrent.CountDownLatch;

public class ThreadArray {

 public static void main(String[] args) {
  CountDownLatch latch = new CountDownLatch(10);
  Thread[]  t = new Thread[10];
  MyThread[] mt = new MyThread[10];

  for(int i =0;i<10;i++) {
   mt[i] = new MyThread(latch);
   t[i] = new Thread(mt[i]);
   
   t[i].start();
   
  }

  try {
   latch.await();
   System.out.println(latch.getCount());
   for(int i =0;i<10;i++) {
    mt[i].stop();
   }
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }



}

class MyThread implements Runnable{

 public volatile boolean exit =true;
 CountDownLatch latch ;
 public MyThread(CountDownLatch latch) {
  // TODO Auto-generated constructor stub
  this.latch =latch;
 }

 @Override
 public void run() {
  
  System.out.println("Running Thread "+Thread.currentThread().getName());
  latch.countDown();
  while(exit) {
//   System.out.println("Running Thread "+Thread.currentThread().getName());
  }
  
  System.out.println("stoping thread "+Thread.currentThread().getName());
 }


 public void stop() {
  exit=false;
 }



}

In Above program, we are actually using Count Down Latch.

We are sharing the latch in every thread and when the thread is running we are decreasing the count of the latch by one.

Here we are using 2 important methods of CountDownLatch class.

latch.countDown(); // this will decrease the count by 1
latch.await() // this will wait till the count of the latch becomes 0;
There may be some other examples of CountDownLatch were present over the internet but I have shared this because it was asked to me in one interview I was given for Java Developer.

If you have any other example in your mind for CountDownLatch, Please share it with us as well by comment below.

Thanks for reading
Noeik




0 comments:

Post a Comment