title: concurrent DB calls using CompletableFuture
body: <p>I am trying to improve performance of the dashboard page of my app .It's a spring boot app and hibernate is used for Dao layer.
  I need to fetch count of participants against 5 different alert type(basically filters).</p>
  <p>I am trying to initiate DB queries concurrently so far I have done this:</p>
  <pre><code>  Map&lt;Long,CompletableFuture&lt;Long&gt;&gt; furtureMap = new HashMap&lt;&gt;();
        Map&lt;Long, Long&gt; alertMap = new HashMap&lt;&gt;();
        if(CollectionUtils.isNotEmpty(alertList)) {
  
          for(AlertMasterI18N alertMasterI18N : alertList) {
            dashboardFilterDto.setAlertId(alertMasterI18N.getAlertMaster().getId());
            setDatesForDashboardFilterDto(dashboardFilterDto);
            CompletableFuture&lt;Long&gt; future = CompletableFuture.supplyAsync(() -&gt; {
              try {
                return dashboardDao.getParticipantsCount(dashboardFilterDto);
              } catch (DaoException e) {
                e.printStackTrace();
              }
              return 0L;
            });
            furtureMap.put(alertMasterI18N.getAlertMaster().getId(), future);
          }
         /* CompletableFuture.allOf(furtureMap.values().toArray(new CompletableFuture[furtureMap.size()])).get(); */
         furtureMap.entrySet().stream().forEach( entry -&gt; {
           entry.getValue().thenAccept( count -&gt; alertMap.put(entry.getKey(),count));
         });
        }
  </code></pre>
  <p>Somehow I am getting quite weird results than that of a results I was getting by executing them sequentially. Same count is returned for all alert sometimes it's 0 while if I revert the code its giving correct results.
  I have also tried using CompletableFuture.allOf and callig get() on it but that time the count for all alerts it consistently 0.</p>
  <p>this first time I am trying my hands on asynch programming
  Could anyone please let me know is there anything I am missing here?</p>