Skip to content

Instantly share code, notes, and snippets.

@mike-neck
Created July 7, 2012 06:40
Show Gist options
  • Save mike-neck/3065146 to your computer and use it in GitHub Desktop.
Save mike-neck/3065146 to your computer and use it in GitHub Desktop.
ExecutorServiceの拡張がうまくできん
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 -> null
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 ->
return delegate.submit (new Callable(){
@Override
Object call() {
return closure()
}
})
}
}
def executor = Executors.newSingleThreadExecutor()
def future = executor.submit {
def list = []
(1..10).each {
println "in Callable ${it}"
list << it
}
return list
}
println "result get -> ${future.get()}"
@fumokmm
Copy link

fumokmm commented Jul 7, 2012

submitはExecutorServiceインタフェースのメソッドのようですので、名前がかぶっているため使えなかったんだと思われます。https://gist.github.com/3065304
と、思ったら既に同じ結論に辿り着かれておられた( https://gist.github.com/3065203 )ようですね、これは失礼^^;

@aya-eiya
Copy link

aya-eiya commented Jul 7, 2012

インタフェースのメソッドなので、実装は継承したクラスで上書きされるみたいですね。
さらに上から塗り替えるメソッドを定義すれば動くようです。
https://gist.github.com/3065503

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