title: Performance Issues with newFixedThreadPool vs newSingleThreadExecutor
body: <p>I am trying to Benchmark our Client code. So I decided I will write a multithreading program to do the benchmarking of my client code. I am trying to measure how much <code>time (95 Percentile)</code> below method will take-</p>
  
  <p><code>attributes = deClient.getDEAttributes(columnsList);</code></p>
  
  <p>So below is the multithreaded code I wrote to do the benchmarking on the above method. I am seeing lot of variations in my two scenarios-</p>
  
  <p>1) Firstly, with multithreaded code by using <code>20 threads</code> and <code>running for 15 minutes</code>. I get 95 percentile as <code>37ms</code>. And I am using-</p>
  
  <pre><code>ExecutorService service = Executors.newFixedThreadPool(20);
  </code></pre>
  
  <p>2) But If I am running my same program for <code>15 minutes</code> using-</p>
  
  <p><code>ExecutorService service = Executors.newSingleThreadExecutor();</code> </p>
  
  <p>instead of</p>
  
  <p><code>ExecutorService service = Executors.newFixedThreadPool(20);</code></p>
  
  <p>I get 95 percentile as <code>7ms</code> which is way less than the above number when I am running my code with <code>newFixedThreadPool(20)</code>.</p>
  
  <p><strong>Can anyone tell me what can be the reason for such high performance issues with-</strong></p>
  
  <p><code>newSingleThreadExecutor vs newFixedThreadPool(20)</code></p>
  
  <p>And by both ways I am running my program for <code>15 minutes</code>.</p>
  
  <p>Below is my code-</p>
  
  <pre><code>public static void main(String[] args) {
  
      try {
  
          // create thread pool with given size
          //ExecutorService service = Executors.newFixedThreadPool(20);
          ExecutorService service = Executors.newSingleThreadExecutor();
  
          long startTime = System.currentTimeMillis();
          long endTime = startTime + (15 * 60 * 1000);//Running for 15 minutes
  
          for (int i = 0; i &lt; threads; i++) {
              service.submit(new ServiceTask(endTime, serviceList));
          }
  
          // wait for termination        
          service.shutdown();
          service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
      } catch (InterruptedException e) {
  
      } catch (Exception e) {
  
      }
  }
  </code></pre>
  
  <p>Below is the class that implements Runnable interface-</p>
  
  <pre><code>class ServiceTask implements Runnable {
  
      private static final Logger LOG = Logger.getLogger(ServiceTask.class.getName());
      private static Random random = new SecureRandom();
  
      public static volatile AtomicInteger countSize = new AtomicInteger();
  
      private final long endTime;
      private final LinkedHashMap&lt;String, ServiceInfo&gt; tableLists;
  
      public static ConcurrentHashMap&lt;Long, Long&gt; selectHistogram = new ConcurrentHashMap&lt;Long, Long&gt;();
  
  
      public ServiceTask(long endTime, LinkedHashMap&lt;String, ServiceInfo&gt; tableList) {
          this.endTime = endTime;
          this.tableLists = tableList;
      }
  
      @Override
      public void run() {
  
          try {
  
              while (System.currentTimeMillis() &lt;= endTime) {
  
                  double randomNumber = random.nextDouble() * 100.0;
  
                  ServiceInfo service = selectRandomService(randomNumber);
  
                  final String id = generateRandomId(random);
                  final List&lt;String&gt; columnsList = getColumns(service.getColumns());
  
                  List&lt;DEAttribute&lt;?&gt;&gt; attributes = null;
  
                  DEKey bk = new DEKey(service.getKeys(), id);
                  List&lt;DEKey&gt; list = new ArrayList&lt;DEKey&gt;();
                  list.add(bk);
  
                  Client deClient = new Client(list);
  
                  final long start = System.nanoTime();
  
                  attributes = deClient.getDEAttributes(columnsList);
  
                  final long end = System.nanoTime() - start;
                  final long key = end / 1000000L;
                  boolean done = false;
                  while(!done) {
                      Long oldValue = selectHistogram.putIfAbsent(key, 1L);
                      if(oldValue != null) {
                          done = selectHistogram.replace(key, oldValue, oldValue + 1);
                      } else {
                          done = true;
                      }
                  }
                  countSize.getAndAdd(attributes.size());
  
                  handleDEAttribute(attributes);
  
                  if (BEServiceLnP.sleepTime &gt; 0L) {
                      Thread.sleep(BEServiceLnP.sleepTime);
                  }
              }
          } catch (Exception e) {
  
          }
      }
  }
  </code></pre>
  
  <p><strong>Updated:-</strong></p>
  
  <p>Here is my processor spec- I am running my program from Linux machine with 2 processors defined as:</p>
  
  <pre><code>vendor_id       : GenuineIntel
  cpu family      : 6
  model           : 45
  model name      : Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz
  stepping        : 7
  cpu MHz         : 2599.999
  cache size      : 20480 KB
  fpu             : yes
  fpu_exception   : yes
  cpuid level     : 13
  wp              : yes
  flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 popcnt aes hypervisor lahf_lm arat pln pts
  bogomips        : 5199.99
  clflush size    : 64
  cache_alignment : 64
  address sizes   : 40 bits physical, 48 bits virtual
  power management:
  </code></pre>