Results 1 to 6 of 6
  1. #1
    pjmorce is offline Member
    Join Date
    Nov 2008
    Posts
    17
    Rep Power
    0

    Default Implement a timeout

    Hello

    I would like to implement a timeout on my java application.

    Imagine that I am calling a method ("test()" for example) that I cannot modify. I want to stop the process if no answer is returned by this method after 60 seconds.

    How can I do that?

    Thanks

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    You want full code or just discussion.

    Timer and TimerTask
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Tic Toc ...

    Taking Nicholas's comment a little further:
    Timer (Java Platform SE 6)
    TimerTask (Java Platform SE 6)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    pjmorce is offline Member
    Join Date
    Nov 2008
    Posts
    17
    Rep Power
    0

    Default

    Thanks, i dont know theses classes but I will check...

    regards

  5. #5
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    ( cannot see your reply )

    Okay, but what you will find is rather deep. Timer and Timer Task try to run - you can make it run for your timeout interval, but better is to do a check of a variable doing Timer / TimerTask.cancel if variable changes to false. In other words, we are coding a softened version of a spin-lock. It sorta depends on your skill level and willingness to code. There are also Threading issues, the Thread class has some sort of done() method, and there are ways of doing a 'stop task' that do not involve hanging the machine.

    test() cannot block unless you want to get into some fancy code. Suggest making test() be in a class with a boolean, pass that class to the Timer Task constructor, which you will subclass Timer Task to do that. Then set the Timer to run at sixty seconds after starting. If test() does not run in a loop and extend Thread, basically what we have here is better approached with Barrier, but the design of that precludes soft-spin loops.

    I could give you some clues but would lead to bad design unless used properly.

    Write test cases, and be sure to run them.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  6. #6
    sujitpal is offline Member
    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Default Implement a Timeout

    I found this forum page with this question because I had almost the same question, and then figured out a pretty nice way of doing this today (based on Brian Goetz's Concurrency in Practice book), which I think is quite elegant, so wanted to share...

    So assume that I have a method SomeClass.someMethod() that returns instanece of SomeOtherClass on which I want to implement a timeout on.

    public class MyCallingClass {
    private static final int TIMEOUT_MILLIS = 5000L; // 5 seconds
    private class TimedWrapper implements Callable<SomeOtherClass> {
    private SomeClass someClass;
    public TimedWrapper(SomeClass someClass) { this.someClass = someClass; }
    public SomeOtherClass call() throws Exception {
    return someClass.someMethod();
    }
    }

    private ExecutorService executor = Executors.newSingleThreadExecutor();

    public void myCallingMethod(SomeClass someClass) {
    long end = System.currentTimeMillis() + TIMEOUT_MILLIS;
    Future<SomeOtherClass> f = executor.submit(new TimedWrapper(someClass));
    try {
    return f.get(end, TimeUnit.MILLISECONDS);
    } catch (ExecutionException e) {
    throw e;
    } catch (TimeoutException e) {
    f.cancel(true);
    }
    }
    }

    HTH
    Sujit

Similar Threads

  1. Connection timeout exception
    By wickedrahul9 in forum Advanced Java
    Replies: 2
    Last Post: 11-13-2008, 08:32 AM
  2. Session Timeout alert message
    By hemendra.rathore in forum Advanced Java
    Replies: 1
    Last Post: 11-03-2008, 07:03 AM
  3. RAD - Error getting connection: Timeout.
    By ShoeNinja in forum Other IDEs
    Replies: 0
    Last Post: 09-09-2008, 05:25 PM
  4. Threads and Timeout in Socket Networking
    By byuu in forum Threads and Synchronization
    Replies: 2
    Last Post: 05-27-2008, 09:05 PM
  5. Detect TimeOut in a HttpURL Connection
    By Ed in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 04:29 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
  •