my dog learned polymorphism
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to time out  :confused: " Watch "How to time out  :confused: " New topic

How to time out :confused:

Srini Kesavan

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?
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

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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

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_
I agree. Here's the link:
subject: How to time out :confused:
It's not a secret anymore!