• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stop a thread executing sequential lines of code

 
Varghese Paul
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

An enhancement I'm working on requires me to execute sequential lines of time-consuming code in a separate thread so that the user has the ability to terminate that particular task.

Now, since I an not using a loop, I cannot use the time-tested "checking- for-a-flag" method to terminate the thread. Also, the javadocs recommend not using the stop() or suspend() functions.

Is there any other way to stop the thread?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ugly, but I've done it before ...
 
Varghese Paul
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody.

This was the solutions that came to mind, but I didn't want to do it because it was very ugly, very unsightly and seems a very inelegant way of doing it.

Is there any Exception which could interrupt the execution-flow? Or is it possible to adapt InterruptedException to handle this situation? How do the sleep() or wait() functions throw the InterruptedException?
 
vu lee
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the long operation doing? Some threads can be stopped, but the others can't. If the long-running thread is waiting for a inputstream form a socket, close the socket. If it is waiting for resultset, hm..hmm. ??? One way to do this is to create a job wrapper which will sleep for sometime, then wake up to check whether the job has been done or user wants to cancel the job. If it's done, return. If the user wants to cancel, interrupt the long-running thread.

This is the code sample

public class ThreadHander {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LongRunningJobWrapper longRunningJob = new LongRunningJobWrapper();
Thread thread = new Thread(longRunningJob);
thread.start();
try{
Thread.sleep(500);
//waiting up to haft of a second
longRunningJob.setStillWaiting(false);

}catch(InterruptedException i){ i.printStackTrace();}
System.out.println("exiting");
}

}
class LongRunningJob implements Runnable{
public void run(){
try{
System.out.println("before sleep");
Thread.sleep(100000);
System.out.println("after sleep");
}catch(InterruptedException i){
i.printStackTrace();
}
}
}

class LongRunningJobWrapper implements Runnable{
volatile boolean stillWaiting = true;
public void setStillWaiting(boolean stillWaiting){
this.stillWaiting = stillWaiting;
System.out.println("in setter " + this.stillWaiting);
}
public void run(){
Thread longRunning = new Thread(new LongRunningJob());
longRunning.start();
try{
while(this.stillWaiting){
Thread.sleep(100);
System.out.println(longRunning.getState());
if(longRunning.getState() == Thread.State.TERMINATED){
return;
}
System.out.println("in while loop: " + this.stillWaiting);
}
longRunning.interrupt();
}catch(InterruptedException i){i.printStackTrace();}

}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic