Groovy fixed worker pool with collected errors
Groovy
import java.util.concurrent.Callable
import java.util.concurrent.Executors
int workers = 4
def tasks = (1..10).collect { int n ->
({ -> if (n == 7) throw new IllegalStateException("job $n failed"); n * n } as Callable)
}
def pool = Executors.newFixedThreadPool(workers)
try {
def futures = pool.invokeAll(tasks)
futures.each { f ->
try {
println "ok ${f.get()}"
} catch (Exception e) {
println "err ${e.cause?.message ?: e.message}"
}
}
} finally {
pool.shutdown()
}