是否有一个Groovy等效的Ruby Timeout模块?
在Ruby中,我会使用Timeout模块,它执行一个块,并在超时时停止执行代码。
require 'timeout' status = Timeout::timeout(5) { # Something that should be interrupted if it takes too much time... }
Groovy有这样的东西吗?
有TimedInterrupt
注释 ,但我还没试过呢……
给它一个快速测试,这个(可怜的例子):
@groovy.transform.TimedInterrupt( 5L ) def loopy() { int i = 0 try { while( true ) { i++ } } catch( e ) { i } } println loopy()
在groovy控制台中运行并在5秒后打印出来。
我明白了:
47314150