It looks to me like something odd is going on with the scope of variables defined outside the closure but referred to from inside, it's not "closing" properly. Does it work any better if you do
def threadPool = Executors.newCachedThreadPool()
List offerLinks = getOfferLinks(parser)
List lista = new ArrayList()
for (enlace in offerLinks) {
println "link " + enlace
lista.add(({ it }.curry(enlace)) as Callable)
}
def futures = threadPool.invokeAll(lista)
futures.each{println "FUTURE " + it.get()}
This should ensure that the right thing gets passed into the closure, and the closure itself doesn't need to refer to the externally-defined enlace
variable directly.
This doesn't on its own explain why what you've already tried didn't work, but it might give you a workaround.
Edit: I didn't spot this before, but I now notice that you aren't declaring enlace
in that for
loop, so it's not a local variable and the closures are (correctly) referring to a single shared variable rather than "closing" over the value in a particular loop iteration. It should work if you use a construction like this instead:
def tasks = offerLinks.collect { link ->
println "link " + enlace
return ({ link } as Callable)
}
def futures = threadPool.invokeAll(tasks)
futures.each{println "FUTURE " + it.get()}
where the link
variable is local to the collect
closure, so the {...} as Callable
will close over the correct value. The equivalent in terms of callAsync
would be to use
List futures = offerLinks.collect { link ->
callAsync { link }
}