Skip to content

Instantly share code, notes, and snippets.

@richmarr
Created April 30, 2012 10:36
Show Gist options
  • Save richmarr/2557166 to your computer and use it in GitHub Desktop.
Save richmarr/2557166 to your computer and use it in GitHub Desktop.
Convenience defer technique for passing execution to a thread pool in Groovy
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}"
}
}
@lospejos
Copy link

It may require adding at the end:

pool?.shutdown()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment