Stopping a Thread in Java? [duplicate]

Assuming your threads are reasonably under your control – i.e. they’re not calling anything which is going to potentially wait forever without your code executing – I would shut it down with a simple (but thread-safe – use volatile!) flag.

See this article for an example in C# – the Java equivalent should be easy to work out. Calling interrupt won’t have any effect until the thread next waits, and stop can leave your app in a hard-to-predict state. Wherever possible, go for a clean, orderly shutdown instead.

Leave a Comment