I google the solution for killing a java thread. And there exists two solutions:
But both of them ar
Since Thread.stop()
is (rightly) deprecated, generally this is accomplished by setting a flag to true. Something like this:
Calling Class:
...
workListExecutor.stop()
...
WorkListExecutor.class:
boolean running = true;
void stop(){
running = false;
}
void run(){
while (running) {
... do my stuff ...
}
}
This is assuming your thread has some kind of main loop (usually the case).
If the code in your loop is too big, you can periodically check if running
is still true
, and bail out if it's not.
This has the advantage that you can still clean up when you're done. The thread will be killed automatically when your run method finishes.