title: How to properly use cyclic behaviour of CyclicBarrier for two different set of tasks?
body: <p>CyclicBarrier executes the barrier action when the last thread enters the barrier. </p>
  
  <p>If there are 5 parties(threads) defined in CyclicBarrier, then when 5th thread(task) will enter the barrier, the barrier will trip (i.e. will reset) and barrier action will get executed.</p>
  
  <p>Here, it does not matter 5th thread is of what type. It could be any task. </p>
  
  <p>So, my questions are: </p>
  
  <ol>
  <li><p>If there are two set of tasks (each of 5 thread), then how to make sure a particular set of tasks gets executed first and then barrier action command gets executed. 
  And the remaining set of tasks gets executed after that and then again barrier action command gets executed.</p></li>
  <li><p>Whether CyclicBarrier is suitable for these kinds of scenarios or not. 
  If not then how its cyclic behavior can be used properly in real-world scenarios. </p></li>
  </ol>
  
  <p>Following is the CyclicBarrier code. </p>
  
  <pre><code>import java.util.concurrent.BrokenBarrierException;
  import java.util.concurrent.CyclicBarrier;
  
  
  public class CyclicBarrierSimpleExample {
      static int barrierActionThreadCount;
      public static void main(String[] args){
          // 5 is the number of parties. So, when the 5th thread will enter the barrier, barrier gets tripped or reset and BarrierAction will be called. 
          final CyclicBarrier cyclicBarrier = new CyclicBarrier(5, new BarrierAction(barrierActionThreadCount));
  
          for(int i=0;i&lt;5;i++){
              Thread validationTask = new Thread(new ValidationTask(i, cyclicBarrier));
              validationTask.start();
          }
  
          for(int i=0;i&lt;5;i++){
              Thread serviceTask = new Thread(new ServiceTask(i, cyclicBarrier));
              serviceTask.start();
          }
      }
  }
  
  class BarrierAction implements Runnable{
      private int barrierActionThreadCount;
      public BarrierAction(int barrierActionThreadCount) {
          this.barrierActionThreadCount=barrierActionThreadCount;
      }
      // Barrier action will execute when barrier is reached i.e. number of parties waiting got executed
      // In this case, it will trip when 5 different threaValidationTaskds are called and then again its number of parties will reset to 5
      @Override
      public void run() {
          this.barrierActionThreadCount++;
          System.out.println("Barrier action thread got executed "+barrierActionThreadCount+" times");
      }
  
  }
  
  
  class ValidationTask implements Runnable{
      CyclicBarrier cyclicBarrier; 
      int threadNum;
      public ValidationTask(int threadNum, CyclicBarrier cyclicBarrier) {
          this.threadNum = threadNum;
          this.cyclicBarrier = cyclicBarrier;
      }
      @Override
      public void run() {
          try {
              Thread.sleep(threadNum*1000);
          } catch (InterruptedException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          }
          System.out.println("Validation Task: Thread-"+threadNum+" got executed");
          try {
  
              cyclicBarrier.await();
          } catch (InterruptedException | BrokenBarrierException e) {
              e.printStackTrace();
          }
  
      }
  
  }
  
  class ServiceTask implements Runnable{
      CyclicBarrier cyclicBarrier; 
      int threadNum;
      public ServiceTask(int threadNum, CyclicBarrier cyclicBarrier) {
          this.threadNum = threadNum;
          this.cyclicBarrier = cyclicBarrier;
      }
      @Override
      public void run() {
          System.out.println("Service Task: Thread-"+threadNum+" got executed");
          try {
              cyclicBarrier.await();
          } catch (InterruptedException | BrokenBarrierException e) {
              e.printStackTrace();
          }
  
      }
  
  }
  </code></pre>
  
  <p>Output of above task: </p>
  
  <pre><code>Validation Task: Thread-0 got executed
  Service Task: Thread-1 got executed
  Service Task: Thread-0 got executed
  Service Task: Thread-4 got executed
  Service Task: Thread-2 got executed
  Service Task: Thread-3 got executed
  Barrier action thread got executed 1 times
  Validation Task: Thread-1 got executed
  Validation Task: Thread-2 got executed
  Validation Task: Thread-3 got executed
  Validation Task: Thread-4 got executed
  Barrier action thread got executed 2 times
  </code></pre>
  
  <p>Behaviour I am expecting is that all validation tasks should be executed first, and thereafter service task should be executed. </p>
  
  <p>Thanks for your help.</p>