Skip to content

Instantly share code, notes, and snippets.

@atomsfat
Created April 21, 2014 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atomsfat/11156843 to your computer and use it in GitHub Desktop.
Save atomsfat/11156843 to your computer and use it in GitHub Desktop.
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