Skip to content

Instantly share code, notes, and snippets.

@nobeans
Created May 31, 2011 15:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nobeans/1000681 to your computer and use it in GitHub Desktop.
Save nobeans/1000681 to your computer and use it in GitHub Desktop.
def worker = { latch, num ->
println "ready: $num"
latch.countDown()
latch.await()
Thread.sleep(num * 10)
println num
}
def latch = new java.util.concurrent.CountDownLatch(args.size())
args.each {
Thread.start worker.curry(latch, it as int)
}
def latch = new java.util.concurrent.CountDownLatch(args.size())
groovyx.gpars.GParsPool.withPool(args.size() ?: 1) {
args.eachParallel {
println "ready: $it"
latch.countDown()
latch.await()
Thread.sleep((it as int) * 10)
println it
}
}
@nobeans
Copy link
Author

nobeans commented May 31, 2011

$ groovy sleepSort.groovy 5 2 4 3 1
ready: 1
ready: 5
ready: 3
ready: 4
ready: 2
1
2
3
4
5

$ groovy sleepSortWithGPars.groovy 5 2 4 3 1
ready: 5
ready: 4
ready: 2
ready: 3
ready: 1
1
2
3
4
5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment