【发布时间】:2012-05-12 03:04:12
【问题描述】:
在 Ruby 中,我会使用 Timeout 模块,它会在该模块中执行一个块,如果超过了超时将停止执行代码。
require 'timeout'
status = Timeout::timeout(5) {
# Something that should be interrupted if it takes too much time...
}
Groovy 有这样的东西吗?
【问题讨论】:
在 Ruby 中,我会使用 Timeout 模块,它会在该模块中执行一个块,如果超过了超时将停止执行代码。
require 'timeout'
status = Timeout::timeout(5) {
# Something that should be interrupted if it takes too much time...
}
Groovy 有这样的东西吗?
【问题讨论】:
有TimedInterruptannotation,不过我还没试过……
快速测试一下,这个(不好的例子):
@groovy.transform.TimedInterrupt( 5L )
def loopy() {
int i = 0
try {
while( true ) {
i++
}
}
catch( e ) {
i
}
}
println loopy()
在 groovy 控制台中运行并在 5 秒后打印出 i。
我明白了:
47314150
【讨论】: