I have a question to Groovy threads.
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.
I wrote the following code, which works:
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)
}
Now, I tried to parallelize this code like this:
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 < numFiles; j += numCores) {
println("j = " + j)
translate(allFiles[j].getAbsolutePath(), targetDir)
}
}
}
}
which does not work and provides the output:
Thread 0 starting
Thread 1 starting
Thread 2 starting
Thread 3 starting
nunCores is 4, and numFiles is 3 in my test case.
What is going on here?
ok, 2 things:
Inside of the thread’s implicit run() method, you are referencing the ‘i’ variable. I’d have to step it through a debugger to see exactly what happens, but technically, you shouldn’t even have access to ‘i’ there, because it’s not final. So, I’d suggest creating a Runnable object to which you pass ‘i’ in the constructor. Then start the runnable on the thread.
Groovy has great concurrency support – no reason to roll your own like this. Take a look at GPars