Created
January 21, 2014 19:30
-
-
Save TioBorracho/8546624 to your computer and use it in GitHub Desktop.
Bug de GPars
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 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