【问题标题】:Is there a Groovy equivalent of the Ruby Timeout module?是否有 Ruby Timeout 模块的 Groovy 等价物?
【发布时间】: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 groovy timeout


    【解决方案1】:

    TimedInterruptannotation,不过我还没试过……

    快速测试一下,这个(不好的例子):

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

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

    我明白了:

    47314150
    

    【讨论】:

      猜你喜欢
      • 2019-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-11
      • 2014-03-08
      • 1970-01-01
      • 2012-05-20
      相关资源
      最近更新 更多