File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Stop a thread executing sequential lines of code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Stop a thread executing sequential lines of code" Watch "Stop a thread executing sequential lines of code" New topic
Author

Stop a thread executing sequential lines of code

Varghese Paul
Greenhorn

Joined: Aug 02, 2004
Posts: 14
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

Joined: Jan 29, 2003
Posts: 8791
Ugly, but I've done it before ...


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Varghese Paul
Greenhorn

Joined: Aug 02, 2004
Posts: 14
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

Joined: Apr 19, 2005
Posts: 189
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();}

}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stop a thread executing sequential lines of code
 
Similar Threads
Handling program termination
Destroying an Applet ?
what are daemon threads?
TIME-OUTING A SOCKET CONNECTION
join