是否有一个Groovy等效的Ruby Timeout模块?

时间:2012-05-02 15:43:33

标签: ruby groovy timeout

在Ruby中,我会使用Timeout模块,它会执行一个块,并在超时时停止执行代码。

require 'timeout'
status = Timeout::timeout(5) {
  # Something that should be interrupted if it takes too much time...
}

Groovy有这样的东西吗?

1 个答案:

答案 0 :(得分:4)

TimedInterrupt annotation,但我还没试过......

给它一个快速测试,这个(可怜的例子):

@groovy.transform.TimedInterrupt( 5L )
def loopy() {
  int i = 0
  try {
    while( true ) {
      i++
    }
  }
  catch( e ) {
    i
  }
}

println loopy()

在groovy控制台中运行并在5秒后打印出i

我明白了:

47314150
相关问题