Prefer modern libraries for concurrency
java.util.concurrent
when dealing with threads.
If you're forced to use a low-level Thread
object, for some reason, then stopping those threads should be done with care.
Threads should not be stopped using stop
or suspend
.
These methods are deprecated.
Instead, threads should use the following simple idiom to stop in a well-behaved manner.
public final class SimpleThread implements Runnable { @Override public void run() { boolean isDone = false; while (!isStopRequested() && !isDone){ //perform the work //if finished, set isDone to true } } /** Request that this thread stop running. */ public synchronized void requestStop(){ isStopRequested = true; } // PRIVATE private boolean isStopRequested; private synchronized boolean isStopRequested() { return isStopRequested; } }