how to kill a thread...
On my tomcat server i have a class that is started on server startup. This class creates a thread in the normal fashion:
Thread thread =new Thread(this);// class implements runnable
I have a method that is called when the server shutsdown. In this method i would like to destroy the thread. This method is in the same class so i have no problem accessing the Thread instance.However i notice most of Thread methods are deprecated i.e. thread.stop() or thread.destroy(). How should i therefore stop the thread?
Thanks in advance.
[652 byte] By [
sanh2005a] at [2007-11-27 2:10:35]

> I have a method that is called when the server> shutsdown. In this method i would like to destroy> the thread.If the server is shutting down, all threads will die. You don't need to explicitly kill that thread.
In other circumstances, the way to kill a thread is to try killing it softly, by invoking Thread's [url=http://java.sun.com/javase/6/docs/api/java/lang/Thread.html#interrupt()]interrupt()[/url] method.
The thread must cooperate by routinely checking its interrupted status, and reacting, of course.
//Nice and friendly like:
boolean running = true;
while(running){
Thread thread = new Thread(this);
}
public void setRunning(boolean running){
this.running = running;
}
Then just call MyClass.setRunning(false);
> //Nice and friendly like:
> boolean running = true;
> while(running) {
>Thread thread = new Thread(this);
> }
> public void setRunning(boolean running){
>this.running = running;
> }
Your code doesn't make sense.
If the program took 5 seconds before calling setRunning(false),
then your code will have created millions of new Threads?
> //Nice and friendly like:
> boolean running = true;
> while(running){
>Thread thread = new Thread(this);
> ublic void setRunning(boolean running){
>this.running = running;
>
> Then just call MyClass.setRunning(false);
You mean
public void run() {
while(running){
// blah blah
}
}
rather than create loads of new threads.
You might also want to make "running" either volatile or access it in a synchronized block.
mlka at 2007-7-12 2:02:51 >

> Your code doesn't make sense.And with no synchronized or volatile, there's no guarantee changes in the variable's value will be propagated between threads.
Anyway, I still think using the interrupted flag is a more standard way toterminate a thread: it interrupts certain blocking methods like wait, whichis a good thing, and doesn't require the coding of ad hoc variables.
My bad.. I am too out of it to think straight.. I'm off to bed, forgive me please.
private Thread thread = new Thread();
public void stop() {
thread = null;
}
public void run() {
Thread thisThread = Thread.currentThread();
while (thread == thisThread) {
try {
//Some action
} catch (InterruptedException e){}
}
}
This was the code I was thinking of.. sorry if any damage was done..
Message was edited by:
grizly
As Dr.Jamf already mentioned, you must make the thread variable "volatile", or use synchronization. Otherwise, the JVM is allowed to "cache" the old value, and thus your loop runs forever.