0
how can stop a thread without using deprecated method?
destroy(), stop() methods are deprecated so please suggest an other option to kill a thread.
1 Odpowiedź
0
For example, suppose your applet contains the following start, stop andrun methods:
private Thread blinker;
public void start() {
blinker = new Thread(this); blinker.start();
}
public void stop() { blinker.stop(); // UNSAFE! } public void run() {
Thread thisThread = Thread.currentThread();
while (true) {
try {
thisThread.sleep(interval);
} catch (InterruptedException e){
}
repaint();
}
}
You can avoid the use of Thread.stop by replacing the applet's stop and run methods with:
private volatile Thread blinker;
public void stop() { blinker = null; }
public void run() {
Thread thisThread = Thread.currentThread();
while (blinker == thisThread) {
try {
vthisThread.sleep(interval);
} catch (InterruptedException e){
}
repaint();
}
}
See : http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html