title: Database insertion is taking zero seconds to insert
body: <p>I am trying to measure the performance of <code>Database Insert</code>. So for that I have written a <code>StopWatch</code> class which will reset the counter before <code>executeUpdate</code> method and calculate the time after <code>executeUpdate</code> method is done.</p>
  
  <p>And I am trying to see how much time each thread is taking, so I am keeping those numbers in a <code>ConcurrentHashMap</code>. </p>
  
  <p>Below is my main class-</p>
  
  <pre><code>public static void main(String[] args) {
  
          final int noOfThreads = 4;
          final int noOfTasks = 100;
  
          final AtomicInteger id = new AtomicInteger(1);
  
          ExecutorService service = Executors.newFixedThreadPool(noOfThreads);
  
          for (int i = 0; i &lt; noOfTasks * noOfThreads; i++) {
              service.submit(new Task(id));
          }
          while (!service.isTerminated()) {
  
          }
  
             //printing the histogram
            System.out.println(Task.histogram);
  
      }
  </code></pre>
  
  <p>Below is the class that implements Runnable in which I am trying to measure each thread performance in inserting to database meaning how much time each thread is taking to insert to database-</p>
  
  <pre><code>class Task implements Runnable {
  
      private final AtomicInteger id;
      private StopWatch totalExecTimer = new StopWatch(Task.class.getSimpleName() + ".totalExec");
      public static ConcurrentHashMap&lt;Long, AtomicLong&gt; histogram = new ConcurrentHashMap&lt;Long, AtomicLong&gt;();
  
      public Task(AtomicInteger id) {
          this.id = id;
      }
  
  
      @Override
      public void run() {
  
          dbConnection = getDBConnection();
  
          preparedStatement = dbConnection.prepareStatement(Constants.INSERT_ORACLE_SQL);
  
          //other preparedStatement
  
          totalExecTimer.resetLap();
  
          preparedStatement.executeUpdate();
  
          totalExecTimer.accumulateLap();
  
          final AtomicLong before = histogram.putIfAbsent(totalExecTimer.getCumulativeTime() / 1000, new AtomicLong(1L));
          if (before != null) {
              before.incrementAndGet();
          }
      }
  }
  </code></pre>
  
  <p>Below is the <code>StopWatch class</code></p>
  
  <pre><code>/**
   * A simple stop watch.
   */
  protected static class StopWatch {
      private final String name;
      private long lapStart;
      private long cumulativeTime;
  
      public StopWatch(String _name) {
          name = _name;
      }
  
      /**
       * Resets lap start time.
       */
      public void resetLap() {
          lapStart = System.currentTimeMillis();
      }
  
      /**
       * Accumulates the lap time and return the current lap time.
       * 
       * @return the current lap time.
       */
      public long accumulateLap() {
          long lapTime = System.currentTimeMillis() - lapStart;
          cumulativeTime += lapTime;
          return lapTime;
      }
  
      /**
       * Gets the current cumulative lap time.
       * 
       * @return
       */
      public long getCumulativeTime() {
          return cumulativeTime;
      }
  
      public String getName() {
          return name;
      }
  
      @Override
      public String toString() {
          StringBuilder sb = new StringBuilder();
          sb.append(name);
          sb.append("=");
          sb.append((cumulativeTime / 1000));
          sb.append("s");
          return sb.toString();
      }
  }
  </code></pre>
  
  <p>After running the above program, I can see 400 rows got inserted. And when it is printing the histogram, I am only seeing like this-</p>
  
  <pre><code>{0=400}
  </code></pre>
  
  <p>which means 400 calls came back in 0 seconds? It's not possible for sure.</p>
  
  <p><strong>I am just trying to see how much time each thread is taking to insert the record and then store those numbers in a <code>Map</code> and print that map from the main thread.</strong> </p>
  
  <p>I think the problem I am assuming it's happening because of thread safety here and that is the reason whenever it is doing <code>resetlap</code> zero is getting set to Map I guess.</p>
  
  <p>If yes how can I avoid this problem? And also it is required to pass <code>histogram map</code> from the main thread to constructor of Task? As I need to print that Map after all the threads are finished to see what numbers are there.</p>
  
  <p><strong>Update:-</strong>
  If I remove <code>divide by 1000</code> thing to store the number as milliseconds then I am able to see some numbers apart from <code>zero</code>. So that looks good. </p>
  
  <p>But One thing more I found out is that numbers are not consistent, If I sum up each threads time, I will get some number for that. And I also I am printing how much time in which whole program is finishing as well. SO I compare these two numbers they are different by big margin</p>