2012-05-02 42 views
2

在Ruby中,我會使用Timeout模塊,它會在其中執行代碼塊,並在代碼超時時停止執行代碼。是否有Ruby超時模塊的Groovy等價物?

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

Groovy有類似的東西嗎?

回答

3

還有就是TimedInterruptannotation,但我還沒有嘗試出來呢......

給它一個快速測試,而這(壞榜樣):

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

println loopy() 

運行在常規控制檯並在5秒後打印出i

我得到:

47314150 
相關問題