Skip to content

Instantly share code, notes, and snippets.

@atomsfat atomsfat/concurrent
Created Apr 21, 2014

Embed
What would you like to do?
concurrent closuere
def concurrent(int count, Closure closure) {
def values = []
def futures = []
ExecutorService executor = Executors.newFixedThreadPool(count)
CyclicBarrier barrier = new CyclicBarrier(count)
for (int i = 0; i < count; i++) {
futures.add(executor.submit(new Callable() {
public def call() throws Exception {
barrier.await()
closure.call()
}
}))
}
for (Future future : futures) {
try {
def value = future.get()
values << value
} catch (ExecutionException e) {
values << e.cause
}
}
return values
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.