Skip to content

Instantly share code, notes, and snippets.

@fumokmm
Forked from nobeans/sleepSort.groovy
Created May 31, 2011 16:58
Show Gist options
  • Save fumokmm/1000878 to your computer and use it in GitHub Desktop.
Save fumokmm/1000878 to your computer and use it in GitHub Desktop.
def worker = { latch, num ->
println "ready: $num"
latch.countDown()
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()
Thread.sleep((it as int) * 10)
println it
}
}
// 自分で理解できるよう噛み砕いて
// コメントなど追加
import static groovyx.gpars.GParsPool.*
import java.util.concurrent.*
def numbers = [8, 1, 4, 9, 3, 6]
def latch = new CountDownLatch(numbers.size())
withPool(numbers.size() ?: 1){ // <- awaitしてるので、数分スレッド確保
numbers.eachParallel {
latch.countDown()
latch.await() // <- 全部の準備が終わるまで待つ
Thread.sleep((it as int) * 10)
println it
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment