Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to limit the execution time of a 3rd party method?

 
Li Yang
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a clean or non-deprecated way to limit the execution time of a method of a 3rd party object?
Explanation:
I encountered a situation in which I need to invoke a method of a 3rd party object that potenially will hang forever.
Using a thread and the stop(), I can limit the execution time of that method. Are there any alternatives?
Thanks.
 
Yegor Bugayenko
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use AOP and a @Timeable annotation from jcabi-aspects:



When time limit is reached your thread will get `interrupted()` flag set to `true` and it's your job to handle this situation correctly and to stop execution.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively, you can run that operation with an Executor, and use a FutureTask to submit the task and wait a maximum amount of time for it to finish. You won't necessarily be able to force the subtask to stop executing, but you can allow the code that was waiting on it to move on.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does twelve years count as a long enough execution time?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic