-
-
Save fumokmm/3065304 to your computer and use it in GitHub Desktop.
ExecutorServiceの拡張がうまくできん -> メソッド名のせいかな?
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 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()}" |
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
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] |
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 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