Skip to content

Instantly share code, notes, and snippets.

@fumokmm
Forked from mike-neck/console.log
Created July 7, 2012 07:54
Show Gist options
  • Save fumokmm/3065304 to your computer and use it in GitHub Desktop.
Save fumokmm/3065304 to your computer and use it in GitHub Desktop.
ExecutorServiceの拡張がうまくできん -> メソッド名のせいかな?
import java.util.concurrent.ExecutorService
import java.util.concurrent.BlockingQueue
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.Executors
import java.util.concurrent.Callable
def executor = Executors.newSingleThreadExecutor()
// Closure as Interfaceパターン!
def future = executor.submit( {
def list = []
(1..10).each {
println "in Callable ${it}"
list << it
}
return list
} as Callable )
println "result get -> ${future.get()}"
in Callable 1
in Callable 2
in Callable 3
in Callable 4
in Callable 5
in Callable 6
in Callable 7
in Callable 8
in Callable 9
in Callable 10
result get -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
import java.util.concurrent.ExecutorService
import java.util.concurrent.BlockingQueue
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.Executors
import java.util.concurrent.Callable
ExecutorService.metaClass.define {
//submit = {Closure closure ->
submit2 = {Closure closure ->
return delegate.submit (new Callable(){
@Override
Object call() {
return closure()
}
})
}
}
def executor = Executors.newSingleThreadExecutor()
//def future = executor.submit {
def future = executor.submit2 {
def list = []
(1..10).each {
println "in Callable ${it}"
list << it
}
return list
}
println "result get -> ${future.get()}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment