title: java, concurrency, executors
body: <p>I have simple class that uses ExecutorService and calculate PI using Monte Carlo method.</p>
  
  <pre><code>  public class MonteCarlo {
  public static void main(String[] args) throws ExecutionException, InterruptedException {
  
      long start = System.currentTimeMillis();
  
      ExecutorService executorService = Executors.newFixedThreadPool(2);
  
      long numberOfWorkers = 1000000;
      double x = 0.0;
      double y = 0.0;
      double radius = 5.0;
      long numberOfAllShoots = 0;
  
      Set&lt;Future&lt;Integer&gt;&gt; futures = new HashSet&lt;&gt;();
      for(int i=0;i&lt;numberOfWorkers;i++) {
          long shootNumber = 500;
          numberOfAllShoots+=shootNumber;
          Callable callable = new Shoot(x,y,radius,shootNumber);
          Future&lt;Integer&gt; future = executorService.submit(callable);
          futures.add(future);
      }
  
      int numberOfCorrectShoots = 0;
      for (Future&lt;Integer&gt; future : futures) {
          numberOfCorrectShoots += future.get();
      }
      executorService.shutdown();
  
      double pi = 4 * ((double)numberOfCorrectShoots / numberOfAllShoots );
  
      System.out.printf("PI is approximated to " + pi);
  
      long end = System.currentTimeMillis();
  
      System.out.println("\n"+(end - start) + " miliseconds ");
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>but the problem is that I get almost the same running time when I set newFixedThreadPool argument high like 100 or 2. The result is the same using newSingleThreadExecutor .
  What is wrong with this code ? I expected that using newFixedThreadPool will be much faster than single thread</p>
  
  <p>EDIT</p>
  
  <p>Shoot class</p>
  
  <pre><code>   public class Shoot implements Callable {
  
  private final double x;
  private final double y;
  private final double radius;
  private final long numberOfShoots;
  
  public Shoot(double x, double y, double radius, long numberOfShoots) {
      this.x = x;
      this.y = y;
      this.radius = radius;
      this.numberOfShoots = numberOfShoots;
  }
  
  @Override
  public Integer call() {
      int numberOfCorrectShoot = 0;
      for(int i=0;i&lt;numberOfShoots;i++) {
          double t = randomDoubleFromRange(x-radius,x+radius);
          double u = randomDoubleFromRange(y-radius,y+radius);
          if((x-t)*(x-t) + (y-u)*(y-u) &lt;= radius * radius) {
              numberOfCorrectShoot++;
          }
      }
      return numberOfCorrectShoot;
  }
  
  private double randomDoubleFromRange(double rangeMin, double rangeMax) {
      Random random = new Random();
      return rangeMin + (rangeMax - rangeMin) * random.nextDouble();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>nproc command return 2</p>
  
  <p>I tried numberOfWorkers = 400 and shootNumber = 1000000 and both newSingleThreadExecutor and newSingleThreadExecutor(2) or more give result about 51000 ms</p>