Example & Tutorial understanding programming in easy ways.

How to stop thread in Java?

Java provides rich APIs for everything but ironically Java doesn't
provide a sure shot way of stopping thread.

There was some control methods in JDK
1.0 e.g. stop(), suspend() and resume() which was deprecated in later releases
due to potential deadlock threats, from then Java API designers has not made any
effort to provide a consistent, thread-safe and elegant way to stop threads.

Programmers mainly rely on the fact that thread stops automatically as soon as they
finish execution of run() or call() method. To manually stop, programmers either
take advantage of volatile boolean variable and check in every iteration if run method
has loops or interrupt threads to abruptly cancel tasks.

Read More →