This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes How to limit the execution time of a 3rd party method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to limit the execution time of a 3rd party method?" Watch "How to limit the execution time of a 3rd party method?" New topic
Author

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

Li Yang
Greenhorn

Joined: Sep 28, 2000
Posts: 7
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

Joined: Feb 11, 2011
Posts: 66
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.


follow me at yegor256.com
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 13, 2005
Posts: 38472
    
  23
Does twelve years count as a long enough execution time?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to limit the execution time of a 3rd party method?