Memory modelmedium0-2 years

How do you cancel a thread, and what must you do when you catch InterruptedException?

There is no way to stop a thread from outside; Thread.stop is gone. Cancellation is cooperative: interrupt() sets a flag, blocking methods throw InterruptedException, and non-blocking code should check isInterrupted(). When you catch it, either exit or restore the flag with Thread.currentThread().interrupt() — swallowing it makes the task un-cancellable.

The lesson behind it →
More on Memory model