title: Junit : Trigger concurrent method calls
body: <p>The objective is to have a unit test to test the concurrent modification exception issue of ArrayList in a method customerservice.registry(). I have to trigger this method concurrently to simulate this.</p>
  <p><strong>Actual Method in the CustomerService Class</strong> :</p>
  <pre><code>List&lt;Customer&gt; customerData = Lists.newArrayList();
  public synchronized void registry(){
  ....
  }
  </code></pre>
  <hr />
  <p><strong>Unit Test</strong> :</p>
  <pre><code>public class concurrencyTest {
      ......
  
      @Test
      public void concurrency() throws InterruptedException {
          int numberOfThreads = 100;
          ExecutorService service = Executors.newFixedThreadPool(10);
          CountDownLatch latch = new CountDownLatch(numberOfThreads);
          AtomicBoolean terminate = new AtomicBoolean(false);
          for (int i = 0; i &lt; numberOfThreads; i++) {
              service.execute(() -&gt; {
                  while (!terminate.get()) {
                      try {
                          customerservice.registry(CustomerData.class);
                      } catch (ConcurrentModificationException e) {
                          endExecution.set(true);
                      }
                      latch.countDown();
                  }
              });
          }
          latch.await();
          assertEquals(terminate.get, false);
      }
  }
  </code></pre>
  <hr />
  <p>The Unit test as mentioned above is the right approach to do so ?</p>