Method Timout??

Discussion in 'Java' started by Marwan, Apr 20, 2004.

  1. Marwan

    Marwan Guest

    Hi all,

    Is there a way to call a method with a timer? Such that if the method
    does not complete before the timer hits 0, the method returns false.

    for example:

    int i=0;
    while ( !methodInvaction() && i++<3 ) {
    }
    boolean methodInvocation() {
    //
    ...
    //
    return true; (2)
    }

    A timer would be added such that if methodInvocation doesn't hit (2)
    in, say, x milliseconds, the method would return false. And 'i' is
    just to have it try upto 3 times.


    Thanks,
    Marwan
     
    Marwan, Apr 20, 2004
    #1
    1. Advertising

  2. > Is there a way to call a method with a timer? Such that if the method
    > does not complete before the timer hits 0, the method returns false.
    >
    > for example:
    >
    > int i=0;
    > while ( !methodInvaction() && i++<3 ) {
    > }
    > boolean methodInvocation() {
    > //
    > ...
    > //
    > return true; (2)
    > }
    >
    > A timer would be added such that if methodInvocation doesn't hit (2)
    > in, say, x milliseconds, the method would return false. And 'i' is
    > just to have it try upto 3 times.


    You might implement it with two threads, one for executing the function and
    one for doing the timing. The one that finishes first should then stop the
    other.

    Niels Dybdahl
     
    Niels Dybdahl, Apr 20, 2004
    #2
    1. Advertising

  3. Niels Dybdahl wrote:

    >>A timer would be added such that if methodInvocation doesn't hit (2)
    >>in, say, x milliseconds, the method would return false. And 'i' is
    >>just to have it try upto 3 times.

    >
    >
    > You might implement it with two threads, one for executing the function and
    > one for doing the timing. The one that finishes first should then stop the
    > other.


    Except that threads cannot be stopped. The stop() method was depreceated and
    is nonfunctional because it could leave the application in an inconsistent
    state.

    Basically, the above is possible only inasmuch as you can let the method run
    on regardless until it (and the thread) terminates naturally. Of course this
    could leave your system swamped with abandoned threads hogging all the CPU
    cycles. This "method timeout" is a nasty hack anyway - if you want your
    stuff to terminate, program it so that it does!
     
    Michael Borgwardt, Apr 20, 2004
    #3
  4. Marwan

    Chris Smith Guest

    Marwan wrote:
    > Is there a way to call a method with a timer? Such that if the method
    > does not complete before the timer hits 0, the method returns false.


    This is only possible with cooperation from the method that's being
    called. Such a method might contain a timeout parameter and check
    System.currentTimeMillis() on a regular basis, for example.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Apr 20, 2004
    #4
  5. In article <c639t8$7h8ge$-berlin.de>,
    Michael Borgwardt <> wrote:
    >
    >Except that threads cannot be stopped. The stop() method was depreceated and
    >is nonfunctional because it could leave the application in an inconsistent
    >state.


    While there is no safe _general_ way of stopping threads, you can
    happily code your own thread in such a way that _it_ can be safely
    stopped. Not by using Thread.stop() though mind you :)

    Cheers
    Bent D
    --
    Bent Dalager - - http://www.pvv.org/~bcd
    powered by emacs
     
    Bent C Dalager, Apr 20, 2004
    #5
  6. Michael Borgwardt wrote:
    > Niels Dybdahl wrote:
    >
    > Except that threads cannot be stopped. The stop() method was depreceated
    > and
    > is nonfunctional because it could leave the application in an inconsistent
    > state.


    Of course they can. You just can't call Thread.stop() to stop them.
    Threads stop when their run() terminates. The key is to cause the run()
    to terminate when you want it to.

    > Basically, the above is possible only inasmuch as you can let the method
    > run
    > on regardless until it (and the thread) terminates naturally. Of course
    > this
    > could leave your system swamped with abandoned threads hogging all the CPU
    > cycles. This "method timeout" is a nasty hack anyway - if you want your
    > stuff to terminate, program it so that it does!


    This question comes up all the time and we always have the "can't call
    stop() on a thread discussion." Write your code so that when the timer
    is through it will cause your run() to end. Thread has three methods to
    make this easy, interrupt(), interrupted(), and isInterrupted(). If you
    are using a Thread.sleep() in your run() you can just exit the method in
    your exception handler. If you are blocked on I/O you can close the
    stream in the timer and exit the run() the same way.

    import java.util.*;

    public class TimedThread implements Runnable {
    Timer timer;
    Thread thread;

    public TimedThread(int delay) {
    timer = new Timer();
    TimerTask timerTask = new TimerTask() {
    public void run() {
    thread.interrupt();
    System.out.println("Timed out!");
    }
    };
    timer.schedule(timerTask,delay);

    thread = new Thread(this);
    thread.start();
    }

    public void run() {
    // this is where your timed code goes
    for (int i=0; i<10000000; i++) {
    if (Thread.interrupted())
    break;
    }
    timer.cancel();
    }

    public static void main(String[] args) {
    new TimedThread(500);
    }
    }

    --

    Knute Johnson
    email s/nospam/knute/
    Molon labe...
     
    Knute Johnson, Apr 20, 2004
    #6
  7. Marwan

    Roedy Green Guest

    On 20 Apr 2004 05:59:55 -0700, (Marwan) wrote or
    quoted :

    >Is there a way to call a method with a timer? Such that if the method
    >does not complete before the timer hits 0, the method returns false.


    that's called a timeout. You will find that sort of code for example
    in a socket read.

    Basically, you set a timer, if the work completes, it turns off the
    timer. If the timer finishes first, it cancels the work, and returns
    an error code.

    See http://mindprod.com/products.html#BUS which includes
    StoppableThread which is a polite way to stop the task that did not
    finish in time. This won't work for a thread that has gone nuts, and
    is hanging in a tight loop.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Apr 21, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bWc=?=

    Session timout problem

    =?Utf-8?B?bWc=?=, Feb 22, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    1,264
    Alvin Bruney [MVP]
    Feb 23, 2004
  2. Ken Varn
    Replies:
    2
    Views:
    334
    Ken Varn
    May 28, 2004
  3. Eyal Sharabi Horwitz

    timout expired

    Eyal Sharabi Horwitz, Jul 29, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    376
    Eyal Sharabi Horwitz
    Jul 29, 2004
  4. Steve Peterson

    Session object timout

    Steve Peterson, Dec 26, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    676
    Steve Peterson
    Dec 27, 2004
  5. Raymond Du

    How to setup session timout

    Raymond Du, Jan 16, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,055
    William F. Robertson, Jr.
    Jan 17, 2005
Loading...

Share This Page