title: Monte Carlo to calculate Pi on multiple Threads
body: <p>I wrote a simple Program in Java to calculate Pi via the Monte Carlo method. Since You need a lot of drops to get Pi 'precise' and it gets, of course, slower I decided to implement Multi-Threading. Now to my question: Is there any way to speed the calculation up? And calculates it only one iteration per physical thread at a time or am I completley wrong in my concept of multithreading?</p>
  
  <p>Here is the code:</p>
  
  <pre><code>public class PiMC{
      public static void main(String[] args) {
          ExecutorService exec=Executors.newCachedThreadPool();
          //Future object is used to get result from a thread
          Future&lt;Double&gt; result0=exec.submit(new Thread1());
          Future&lt;Double&gt; result1=exec.submit(new Thread1());
          Future&lt;Double&gt; result2=exec.submit(new Thread1());
          Future&lt;Double&gt; result3=exec.submit(new Thread1());
          Future&lt;Double&gt; result4=exec.submit(new Thread1());
          Future&lt;Double&gt; result5=exec.submit(new Thread1());
          Future&lt;Double&gt; result6=exec.submit(new Thread1());
          Future&lt;Double&gt; result7=exec.submit(new Thread1());
  
          try
          {
              System.out.println(((result0.get() + result1.get() + result2.get() + result3.get() + result4.get()+ result5.get() + result6.get() + result7.get()) / Long.MAX_VALUE) * 4);
          }
          catch(InterruptedException e){System.out.println(e);}
          catch(ExecutionException e){System.out.println(e);}
      }
  }
  
  class Thread1 implements Callable {
      @Override
      public Double call() throws Exception {
          long drops = Long.MAX_VALUE / 8;
          //long drops = 500;
          double in = 0;
          for (long i = 0; i &lt;= drops; i++) {
              double x = Math.random();
              double y = Math.random();
              if (x * x + y * y &lt;= 1) {
                  in++;
              }
          }
          return in;
      }
  }
  </code></pre>