Created
April 21, 2014 21:16
-
-
Save atomsfat/11156843 to your computer and use it in GitHub Desktop.
concurrent closuere
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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