title: Groovy concurrency
body: <p>I have a question to Groovy threads.</p>
  
  <p>My task is to translate each file in a given directory in a certain manner
  and place the resulting output in a file in an other directory.</p>
  
  <p>I wrote the following code, which works:</p>
  
  <pre><code>static def translateDir(fromDir, targetDir) {
      def allFiles = new File(fromDir).listFiles()
      def numFiles = allFiles.length
  
      for (i in 0..(numFiles - 1))
          translate(allFiles[i].getAbsolutePath(), targetDir)
  }
  </code></pre>
  
  <p>Now, I tried to parallelize this code like this: </p>
  
  <pre><code>static def translateDir(fromDir, targetDir) {
      def allFiles = new File(fromDir).listFiles()
      def numFiles = allFiles.length
      def numCores = Runtime.getRuntime().availableProcessors()
  
      for (i in 0..(numCores - 1)) {
          println("Thread " + i + "starting")
          Thread.start {
              for (def j = i; j &lt; numFiles; j += numCores) {
                  println("j = " + j) 
                  translate(allFiles[j].getAbsolutePath(), targetDir)
              }
          }
      }
  }
  </code></pre>
  
  <p>which does not work and provides the output:</p>
  
  <pre><code>Thread 0 starting
  Thread 1 starting
  Thread 2 starting
  Thread 3 starting
  </code></pre>
  
  <p>nunCores is 4, and numFiles is 3 in my test case.
  What is going on here?</p>