how can stop a thread without using deprecated method? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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.

12th Sep 2016, 1:38 AM
Ravi Shankar Saini
Ravi Shankar Saini - avatar
1 Answer
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
12th Sep 2016, 3:31 AM
WPimpong
WPimpong - avatar