Created
April 30, 2012 10:36
-
-
Save richmarr/2557166 to your computer and use it in GitHub Desktop.
Convenience defer technique for passing execution to a thread pool in Groovy
This file contains hidden or 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 java.util.concurrent.Callable | |
import java.util.concurrent.Executors | |
def THREADS = 2 | |
def pool = Executors.newFixedThreadPool(THREADS) | |
def done = 0 | |
def defer = { closure -> | |
pool.submit( closure as Callable ) | |
} | |
for ( def i = 0; i < 10; i++ ) { | |
def j = i+1 | |
defer { | |
Thread.sleep(1000) | |
println "Executing task ${j}" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It may require adding at the end: