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 time out  :confused: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to time out  :confused: " Watch "How to time out  :confused: " New topic
Author

How to time out :confused:

Srini Kesavan
Greenhorn

Joined: Jan 18, 2003
Posts: 18
I would like to exit out of the program if a method call is taking too much time to process.
How can I do that?
Example:-
IData outputDocument = invoke(context, inputDocument);
if the invoke is taking more than 10 secs. I would like to exit out of the program.
Any help will be useful.
Thanks in advance


SCDJWS,SCWCD,SCJP
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Immediately before calling the long-running method, start a new Thread running which sleeps for the longest possible time, then exits the program. When the method completes, stop the thread.

If t.interrupt() is called within 10 seconds, the app will continue; otherwise it will exit.


[Jess in Action][AskingGoodQuestions]
Srini Kesavan
Greenhorn

Joined: Jan 18, 2003
Posts: 18
Thanks for your help. Really appreciate it.
Gaurav Mac Mathur
Ranch Hand

Joined: Feb 19, 2002
Posts: 47
I agree this possibally teh best possible solution, but let me warn you that the Sleep time may not be what you prescribe in the code, As thread prioriy are JVM implementation dependent.
_based on my Theoratical Knowledge_
Cheers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to time out :confused: