-
-
Save fumokmm/1000878 to your computer and use it in GitHub Desktop.
This file contains 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 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) | |
} |
This file contains 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 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 | |
} | |
} |
This file contains 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
// 自分で理解できるよう噛み砕いて | |
// コメントなど追加 | |
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