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