title: Code inside thread slower than outside thread..?
body: <p>I'm trying to alter some code so it can work with multithreading. I stumbled upon a <strong>performance loss when putting a Runnable around some code</strong>.</p>
  
  <p>For clarification: The original code, let's call it</p>
  
  <pre><code>//doSomething
  </code></pre>
  
  <p>got a Runnable around it like this:</p>
  
  <pre><code>Runnable r = new Runnable()
  {
      public void run()
      {
          //doSomething
      }
  }
  </code></pre>
  
  <p>Then I submit the runnable to a ChachedThreadPool ExecutorService. This is my first step towards multithreading this code, to see if the code runs as fast with one thread as the original code.</p>
  
  <p>However, this is not the case. Where <em>//doSomething</em> executes in about 2 seconds, the Runnable executes in about 2.5 seconds. <b> I need to mention that some other code, say, <em>//doSomethingElse</em>, inside a Runnable <strong>had no performance loss</strong> compared to the original <em>//doSomethingElse</em>. </b></p>
  
  <p>My guess is that <em>//doSomething</em> has some operations that are not as fast when working in a Thread, but I don't know what it could be or what, in that aspect is the difference with <em>//doSomethingElse</em>.</p>
  
  <p>Could it be the use of <strong>final int[]/float[]</strong> arrays that makes a Runnable so much slower? The <em>//doSomethingElse</em> code also used some finals, but <em>//doSomething</em> uses more. This is the only thing I could think of.</p>
  
  <p>Unfortunately, the <em>//doSomething</em> code is quite long and out-of-context, but I will post it here anyway. For those who know the Mean Shift segmentation algorithm, this a part of the code  where the mean shift vector is being calculated for each pixel. The for-loop </p>
  
  <pre><code>for(int i=0; i&lt;L; i++) 
  </code></pre>
  
  <p>runs through each pixel.</p>
  
  <pre><code>timer.start(); // this is where I start the timer
  // Initialize mode table used for basin of attraction
  char[] modeTable = new char [L]; // (L is a class property and is about 100,000)
  Arrays.fill(modeTable, (char)0);
  int[] pointList = new int [L];
  
  // Allcocate memory for yk (current vector)
  double[] yk = new double [lN]; // (lN is a final int, defined earlier)
  
  // Allocate memory for Mh (mean shift vector)
  double[] Mh = new double [lN];
  
  int idxs2 = 0; int idxd2 = 0;
  for (int i = 0; i &lt; L; i++) {
      // if a mode was already assigned to this data point
      // then skip this point, otherwise proceed to
      // find its mode by applying mean shift...
      if (modeTable[i] == 1) {
          continue;
      }
  
      // initialize point list...
      int pointCount = 0;
  
      // Assign window center (window centers are
      // initialized by createLattice to be the point
      // data[i])
      idxs2 = i*lN;
      for (int j=0; j&lt;lN; j++)
      yk[j] = sdata[idxs2+j]; // (sdata is an earlier defined final float[] of about 100,000 items)
  
      // Calculate the mean shift vector using the lattice
      /*****************************************************/
      // Initialize mean shift vector
      for (int j = 0; j &lt; lN; j++) {
          Mh[j] = 0;
      }
      double wsuml = 0;
      double weight;
  
      // find bucket of yk
      int cBucket1 = (int) yk[0] + 1;
      int cBucket2 = (int) yk[1] + 1;
      int cBucket3 = (int) (yk[2] - sMinsFinal) + 1;
      int cBucket = cBucket1 + nBuck1*(cBucket2 + nBuck2*cBucket3);
      for (int j=0; j&lt;27; j++) {
          idxd2 = buckets[cBucket+bucNeigh[j]]; // (buckets is a final int[] of about 75,000 items)
          // list parse, crt point is cHeadList
          while (idxd2&gt;=0) {
              idxs2 = lN*idxd2;
              // determine if inside search window
              double el = sdata[idxs2+0]-yk[0];
              double diff = el*el;
              el = sdata[idxs2+1]-yk[1];
              diff += el*el;
              //...
              idxd2 = slist[idxd2]; // (slist is a final int[] of about 100,000 items)
          }
      }
      //...
  }
  timer.end(); // this is where I stop the timer.
  </code></pre>
  
  <p>There is more code, but <strong>the last while loop</strong> was where I first noticed the difference in performance.</p>
  
  <p>Could anyone think of a reason why this code runs slower inside a Runnable than original?</p>
  
  <p>Thanks.</p>
  
  <p><b>Edit: The measured time is <em>inside</em> the code, so <em>excluding</em> startup of the thread.</b></p>