Results 1 to 7 of 7
  1. #1
    toofast is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Calling a method with a timeout

    Hi everybody, I have a line code like this one:

    String result = someHeavyMethod();

    and I need to add this logic: "if the method doesn't finish within 10 seconds, cancel it and let me continue with the rest of the program".

    Is there a way to accomplish this? I'd really appreciate any suggestions.

    PS: I'm forced to use Java 1.4.2

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,650
    Rep Power
    5

    Default Re: Calling a method with a timeout

    Seems to me that your method could invoke a try catch block on an interrupted exception. Then just
    before you call the method, kick off a thread with a timer to interrupt the method in the other thread.
    I would have to play around with this to get it to work but I am pretty certain something along this
    line can be done.

    And just curious why you (or your organization) are still using Java 1.4? I would at least go with Java 1.5
    and Generics.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Calling a method with a timeout

    Quote Originally Posted by jim829 View Post
    Seems to me that your method could invoke a try catch block on an interrupted exception. Then just
    before you call the method, kick off a thread with a timer to interrupt the method in the other thread.
    I would have to play around with this to get it to work but I am pretty certain something along this
    line can be done.
    There's a gotcha here: if that other thread is blocked in an ordinary IO operation (InputStream and OutputStream etc.), all the interrupt() method does is set a flag. The thread itself has to check this flag, after it has finished its blocking operation (which can be hours after the event). They should've retrofitted the InterruptableChannels in the old IO framework (if possible) ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    toofast is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: Calling a method with a timeout

    First of all, I tried coding an example to see if I understand the whole point. This way I can play with the duration of each thread by using the "sleep" method. Here's what a I got so far:

    Java Code:
    public void mainMethod() {
            final Thread main = Thread.currentThread();
    
            Thread timer = new Thread() {
                public void run() {
                    try {
                        this.sleep(3000);
                        main.interrupt();
                    } catch (InterruptedException e) {
                    }
                }
            ;
            };
            
            heavyTestMethod();
            timer.interrupt();
    }
    
    public void heavyTestMethod() {
            try {
                Thread.currentThread().sleep(5000);
            } catch (InterruptedException ex) {
            }
    }
    But for now, it doesn't work. The timer doesn't interrupt the main thread. What am I doing wrong?

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,049
    Rep Power
    6

    Default Re: Calling a method with a timeout

    Well I don't see you actually starting the timer thread anywhere.

    I also wonder how you know what is going on anywhere, you are only catching and ignoring the interrupt exceptions. I'd expect a whole lot of System.out statements in that code to know what happens and when.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    toofast is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: Calling a method with a timeout

    Ok, I just forgot to start the timer. This happens on mondays.

    Now I think it works (I DO have System.out statements everywhere, I just removed them to make the post clearer), I'll do some more tests to be sure.

    Thank you very much!

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,049
    Rep Power
    6

    Default Re: Calling a method with a timeout

    Quote Originally Posted by toofast View Post
    Ok, I just forgot to start the timer. This happens on mondays.
    Nice excuse mr. "toofast" ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 11:40 AM
  2. Calling The main method from another method
    By SwissR in forum New To Java
    Replies: 3
    Last Post: 07-27-2010, 11:03 AM
  3. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 05:30 PM
  4. Timeout for a method execution
    By pjmorce in forum Advanced Java
    Replies: 2
    Last Post: 12-22-2008, 06:51 PM
  5. Replies: 29
    Last Post: 09-25-2008, 07:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •