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 How to Kill a running thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to Kill a running thread" Watch "How to Kill a running thread" New topic
Author

How to Kill a running thread

Hunter Dash
Greenhorn

Joined: Nov 25, 2000
Posts: 12
Hi all,
Need some help here. How do we kill a running thread after elapsing a settime say about 2 minutes?
I know we could use the Interrupt method of the thread to stop a running method but i am not sure how i can stop after a period of time.
Any help is appreciated.
Jim Baiter
Ranch Hand

Joined: Jan 05, 2001
Posts: 532
Try checking out http://java.sun.com/docs/books/tutorial/essential/threads/timer.html
They added a Timer and TimerTask classes in 1.3 that may help
you. I haven't tried them - usually I grew my own by storing
the time when I entered my run() method and checked on each
iteration of a loop returning when I'd exceeded the desired time.
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1836
Hi,
What you can do is check the current time by using System.currentTimeMillies() which returns a long. After this you can use the normal

you can check the System.currentTimeMillies() again in the while loop and interrupt the thread when the specified time has elapsed. If you do not get it i will write down the code.
You cannot use stop() however since it has been deprecated.


[This message has been edited by Rahul Mahindrakar (edited January 10, 2001).]
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Rahul,
The <code>interrupt()</code> method is not guaranteed to stop the thread. The best way ( and what Sun recommends ) to kill a running thread is to use a state-based logic, like a flag or something that can be inspected in the run method. You will have to do this twice in your run method, once before entering into the method using a if-loop and then inside the method at regular intervals using some kind of polling logic.
Ajith

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1836
Ajith,
I agree with you when you say
interrupt() method is not guaranteed to stop the thread.
Thus one cannot "Kill" a thread once a specific period elapsed. However one can stop the work to be done safely as given in the code below

[This message has been edited by Rahul Mahindrakar (edited January 10, 2001).]
Hunter Dash
Greenhorn

Joined: Nov 25, 2000
Posts: 12
Take a look at the below code. now i want to stop an instance of runthread if the time exceeds a setlimit (in this case 2000 seconds)

when i run this program, Thread.currentThread().interrupt(); is not stopping the thread. what is the workaround?
/***************************
public class Runnner{
public Runnner() {
}
public static void main(String[] args) {
Runnner tea = new Runnner();
tea.run();
}

public void run() {
for (int i=0; i < 4; i++)
new runThread().start();
}

}
}

/*********************************************
import java.util.Timer;
import java.util.TimerTask;

public class runThread extends Thread {

Timer timer;
public runThread() {
//
}
class RemindTask extends TimerTask {
public void run() {
Thread.currentThread().interrupt();
timer.cancel(); //Terminate the timer thread
}
}
public void runCrawler() {
try {
//Start the timer
timer = new Timer();
timer.schedule(new RemindTask(), 2000);


runSomething() ; //This may take more than 2000 seconds
//If it exceeds i want to kill this
//thread not jvm.
} catch ( Exception e ) {
System.out.println( "Message: " + e.getMessage() );
e.printStackTrace();
}
}
public void run() {
try {
runCrawler();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1836
Hi,
you have not enclosed your run method in a while loop which checks whether interrupt has been raized. Please refer to my code again especially this part.




[This message has been edited by Rahul Mahindrakar (edited January 10, 2001).]
Hunter Dash
Greenhorn

Joined: Nov 25, 2000
Posts: 12
Thanks for the reply Rahul. But i must be missing some thing. take a look at the code in my previous posting.
The runSomething() method in the RunCrawler() method may take more than specified amount of time. I want to stop the thread when if it takes more than specified time.

Using the the new timerTask and timer classes combo i am able to get the the thread interrupted. But i am unable to stop the thread and it continues to execute.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Kill a running thread
 
Similar Threads
Kill a thread
thread
How to kill Threads
Help !!
How to kill a user thread forcefully?