File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP 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: 77
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.


SCEA, PMP, read my blog: www.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: 39478
    
  28
Does twelve years count as a long enough execution time?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to limit the execution time of a 3rd party method?