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

    Default Timeout for a method execution

    Hello

    I would like to have some help about an issue:

    I have a method "doSomething" which can take a while to be fully executed.

    ...
    boolean bResult = false;
    bResult = doSomething();
    System.out.println("Result:" + bResult);
    ...
    I would like to be able to stop the method execution after 10 seconds and continue the normal execution of my class. This means:

    - If doSomething() is executed in 3 seconds for example, the System.out.println() statement shows the result of the method
    - If doSomething() is executed in more than 10 seconds, after 10 seconds the method is stopped and the System.out.println() will always have false as result.

    Anyone can help me about this?

    Thanks

    regards

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

    Default Clarify

    What does the doSomething method do?
    • Is it calling another method?
    • Is it calling/waitng on a process?
    • Something else?

    Depending on the answer the solution could be different.

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

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default Stopping a long running method

    I can suggest two options.

    1. Within the method, assuming it is looping and not waiting for an external event, add a local field and test the time each time around the loop.

    Java Code:
    void method() {
    	long endTimeMillis = System.currentTimeMillis() + 10000;
    	while (true) {
    		// method logic
    		if (System.currentTimeMillis() > endTimeMillis) {
    			// do some clean-up
    			return;
    		}
    	}
    }
    2. Run the method in a thread, and have the caller count to 10 seconds.

    Java Code:
    	Thread thread = new Thread(new Runnable() {
    		@Override
    		public void run() {
    			method();
    		}
    	});
    	thread.start();
    	long endTimeMillis = System.currentTimeMillis() + 10000;
    	while (thread.isAlive()) {
    		if (System.currentTimeMillis() > endTimeMillis) {
    			// set an error flag
    			break;
    		}
    		try {
    			Thread.sleep(500);
    		}
    		catch (InterruptedException t) {}
    	}
    The drawback to this approach is that method() cannot return a value directly, it must update an instance field to return its value.

    -Steve

Similar Threads

  1. Implement a timeout
    By pjmorce in forum Advanced Java
    Replies: 5
    Last Post: 04-27-2010, 02:36 AM
  2. session timeout
    By nmbalaji in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 12-12-2008, 04:04 PM
  3. RAD - Error getting connection: Timeout.
    By ShoeNinja in forum Other IDEs
    Replies: 0
    Last Post: 09-09-2008, 05:25 PM
  4. Method execution time
    By javaplus in forum Advanced Java
    Replies: 3
    Last Post: 11-26-2007, 09:51 AM

Posting Permissions

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