Skip to content

Instantly share code, notes, and snippets.

@TioBorracho
Created January 21, 2014 19:30
Show Gist options
  • Save TioBorracho/8546624 to your computer and use it in GitHub Desktop.
Save TioBorracho/8546624 to your computer and use it in GitHub Desktop.
Bug de GPars
import groovyx.gpars.GParsPool
def set = (1..10)
def cnt = 0
def r = new java.util.Random()
def baseThreads = Thread.activeCount()
GParsPool.withPool(3) {
println "Only 3 \"Starting thread\" call should exists until one \"Closing\""
set.eachParallel{ aa ->
synchronized(this.class) {
cnt += 1
println "Starting thread ${Thread.currentThread()}. Active threads: counter -> $cnt activeThreads -> ${(Thread.activeCount() - baseThreads)}"
}
sleep(100 + (r.nextInt() % 10))
synchronized(this.class) {
cnt -= 1
println "Closing thread ${Thread.currentThread()}. Active threads: counter -> $cnt activeThreads -> ${(Thread.activeCount() - baseThreads)}"
}
}
}
GParsPool.withPool(3) {
println "Same code with an extra pool nested. Everything goes bananas"
set.eachParallel{ aa ->
synchronized(this.class) {
cnt += 1
println "Starting thread ${Thread.currentThread()}. Active threads: counter -> $cnt activeThreads -> ${(Thread.activeCount() - baseThreads)}"
}
sleep(100 + (r.nextInt() % 10))
GParsPool.withPool(2) {
set.eachParallel { elem ->
sleep(100 + (r.nextInt() % 100))
}
}
synchronized(this.class) {
cnt -= 1
println "Closing thread ${Thread.currentThread()}. Active threads: counter -> $cnt activeThreads -> ${(Thread.activeCount() - baseThreads)}"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment