【发布时间】:2014-05-27 16:30:57
【问题描述】:
在 Groovy 中我们可以做到这一点...
def pool = Executors.newFixedThreadPool(1)
def defer = { c -> pool.submit(c as Callable) }
def myfunction = {"hello"}
def deferedInvocation = defer(myfunction)
def response = deferedInvocation.get()
println response // outputs hello
我想做的是让 defer 闭包将其关闭的变量之一传递到它调用的闭包 c 中。
所以我们有一个外部变量tony
类似这样的:
def tony = "tony"
def pool = Executors.newFixedThreadPool(1)
def defer = { c -> pool.submit(c(tony) as Callable) } // tony is closed variable
def myfunction = {"hello " + it}
def deferedInvocation = defer(myfunction)
def response = deferedInvocation.get()
println response
但是,当我尝试得到:
java.util.concurrent.ExecutionException: org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack: No signature of method: java.lang.String.call() is applicable for argument types: () values: []
Possible solutions: wait(), any(), wait(long), each(groovy.lang.Closure), any(groovy.lang.Closure), take(int)
有什么想法吗?
【问题讨论】: