Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8218157

Cyclic Barrier not working as expected

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Java 8 (152)/WIn 10/HP probook

      A DESCRIPTION OF THE PROBLEM :
      I have created cyclic barrier for 5 parties but my program stuck for infinite waiting state. Cyclic barrier not working as per expectation.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Code provided below to run code.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Output should come after all thread execution but program got stuck in waiting state. Cyclic barrier should move after parties reached to await state.
      ACTUAL -
      Program stuck and cyclic barrier not working as expected

      ---------- BEGIN SOURCE ----------

      import java.util.concurrent.CyclicBarrier;
      import java.util.concurrent.ExecutorService;
      import java.util.concurrent.Executors;

      public class myMain {

      public static void main(String[] args)
      {
      CyclicBarrier latch = new CyclicBarrier(5, new Runnable() {

      @Override
      public void run() {
      System.out.println("barrier broken");

      }
      });
      ExecutorService executer = Executors.newFixedThreadPool(100);
      for(int i=0;i<10;i++)
      {
      Runnable worker = new barrier(latch);
      executer.execute(worker);
      }
      executer.shutdown();

      while (!executer.isTerminated()) {
              }
      }
       
      }

      import java.util.concurrent.BrokenBarrierException;
      import java.util.concurrent.CyclicBarrier;

      public class barrier implements Runnable{

      CyclicBarrier cBar;
      barrier(CyclicBarrier cycBar)
      {
      this.cBar = cycBar;
      }
      @Override
      public void run() {
      System.out.println("I am using cyclic start " + Thread.currentThread().getName().toString());
      try {
      cBar.await();
      System.out.println("I am using cyclic finish 1st " + Thread.currentThread().getName().toString());
      cBar.await();
      System.out.println("I am using cyclic finish 2nd " + Thread.currentThread().getName().toString());
      cBar.await();
      System.out.println("I am using cyclic finish 3rd " + Thread.currentThread().getName().toString());
      } catch (InterruptedException e) {
      e.printStackTrace();
      } catch (BrokenBarrierException e) {
      System.out.println("I am broken exception");
      e.printStackTrace();
      }
      }

      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Thread poll size reduce to 5.

      FREQUENCY : always


            martin Martin Buchholz
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: