• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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

 
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.
 
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.
 
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.
 
Marshal
Posts: 61690
192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does twelve years count as a long enough execution time?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!