Stop threads through cooperation
In general, you should use modern libraries such as 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(){ fIsStopRequested = true; } // PRIVATE private boolean fIsStopRequested; private synchronized boolean isStopRequested() { return fIsStopRequested; } }
See Also :
Would you use this technique?