Setting a maximum execution time for a method/thread

前端 未结 2 1414
一整个雨季
一整个雨季 2020-12-09 11:07

I have a method, which writes to the database. The requirement is to make sure that this method does not execute after a certain time elapses. If it returns before that, the

2条回答
  •  时光取名叫无心
    2020-12-09 12:06

    There is also an AspectJ solution for that with jcabi-aspects library:

    @Timeable(limit = 5, unit = TimeUnit.SECONDS)
    public String writeToDb() {
      // writeToDb
    }
    

    There is an article explaining it further: Limit Java Method Execution Time

提交回复
热议问题