how to timeout a command - Java

This is a discussion on how to timeout a command - Java ; You better read about Threads. Here is short example. It's not good enough for real application (because it waits 2 seconds anyway) but it's good for start. public class Controller { public static void main(String[] args) { Executer ex=new Executer(); ...

+ Reply to Thread
Results 1 to 2 of 2

how to timeout a command

  1. Default Re: how to timeout a command

    You better read about Threads.
    Here is short example. It's not good enough for real application (because it
    waits 2 seconds anyway)
    but it's good for start.
    public class Controller {

    public static void main(String[] args) {
    Executer ex=new Executer();
    ex.setPriority(Thread.currentThread().getPriority()-1);
    ex.start();
    try {
    Thread.sleep(2000);
    ex.enough();
    } catch (InterruptedException e) {
    }

    System.out.println("Last step was "+ex.i);
    }
    }
    public class Executer extends Thread{
    public int i;
    boolean notEnough=true;
    public void run(){
    System.out.println("Start execution ");
    for (i=0;notEnough&&i<1000000000;i++) System.out.println("execute "+i);
    System.out.println("End execution ");
    }
    public void enough(){
    notEnough=false;
    }
    }


    You have to read Threads by yourself.
    So you will know about synchronization and can modify this code for better
    results

    Alex Kizub.

    wai yung wrote:

    > Hi all,
    > I need to execute a command, a method of an object, which will run for an
    > uncertain amount of time. It could be a long time. So what I want to do is
    > if the command does not return , say , in two or three seconds, I would like
    > to terminate it and move on. Is there any way I can do something like that?
    > Thank you for your help.
    > Wai



  2. Default how to timeout a command

    Hi all,
    I need to execute a command, a method of an object, which will run for an
    uncertain amount of time. It could be a long time. So what I want to do is
    if the command does not return , say , in two or three seconds, I would like
    to terminate it and move on. Is there any way I can do something like that?
    Thank you for your help.
    Wai



+ Reply to Thread

Similar Threads

  1. Replies: 8
    Last Post: 09-25-2008, 01:23 PM
  2. command timeout - SQL 2005 express - critical customer down
    By Application Development in forum ADO DAO RDO RDS
    Replies: 4
    Last Post: 12-05-2007, 02:45 PM
  3. Command timeout in MS DAAB
    By Application Development in forum ADO DAO RDO RDS
    Replies: 0
    Last Post: 09-23-2007, 08:43 PM
  4. Replies: 0
    Last Post: 05-23-2007, 06:55 AM
  5. Replies: 1
    Last Post: 01-03-2004, 04:17 PM