Possible Duplicate:
How to kill a java thread ?
I have threads in my app like the below one.
How can I terminate them?
new Thread(new Runnable() {
public void run() {
// do it…
}
}).start();
Edit:
Solution:
Class Memory{
static Runnable last;
}
This will save our Thread in a var.
new Thread(new Runnable() {
public void run() {
Memory.last = this;
// do it…
开发者_运维技巧 }
}).start();
now in any part you want stop:
Memory.last.wait(); //will pause but next new thread will terminate it (garbage collector do this );
Inside your run() method, you can periodically call interrupted()
to test whether the thread is interrupted, and then either exit silently or throw an InterruptedException. To stop the thread you can call it's interrupt()
method. See the Java tutorial in interrupts for more info.
They'll stop running when it's run method is done. Depending on what you put in that method they'll stop immediately or would run forever.
If they have a loop you have to change that thread condition to false.
精彩评论