Is there a Groovy equivalent of the Ruby Timeout module?

前端 未结 1 1301
孤独总比滥情好
孤独总比滥情好 2021-01-06 20:38

In Ruby I would use the Timeout module, where it executes a block and will stop executing the code if it passes the timeout.

require \'timeout\'
status = Tim         


        
相关标签:
1条回答
  • 2021-01-06 20:57

    There is the TimedInterrupt annotation, but I've not tried it out yet...

    Gave it a quick test, and this (poor example):

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

    Runs in the groovy console and prints out i after 5 seconds.

    I get:

    47314150
    
    0 讨论(0)
提交回复
热议问题