Interrupting Threads
A thread terminates when its run method returns. In JDK 1.0, there also was a stop method that another thread could call to terminate a thread. However, that method is now deprecated. We discuss the reason on page 46.
There is no longer a way to force a thread to terminate. However, the interrupt method can be used to request termination of a thread.
When the interrupt method is called on a thread, the interrupted status of the thread is set. This is a Boolean flag that is present in every thread. Each thread should occasionally check whether it has been interrupted.
To find out whether the interrupted status was set, first call the static Thread.currentThread method to get the current thread and then call the isInterrupted ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access