I think killing a thread
at precisely the time you want is a problem for many people. I recently ran across a way to kill a thread
by throwing an exception
. What are your thoughts/inputs on this? Has anyone implemented something like this before?

- 2,605
- 3
- 29
- 49

- 1,037
- 4
- 19
- 27
-
1Where did you get the idea that killing a thread is a problem for many people? – Kayaman Dec 03 '14 at 07:47
-
You can use/call the Thread.interrupt() method to terminate the thread. – Rakesh Soni Dec 03 '14 at 07:50
-
1@RakeshSoni No you can't (at least not any given thread), but you can use `Thread.stop()`. The thing is, you shouldn't. – Kayaman Dec 03 '14 at 07:51
-
Please note thread.stop() has been deprecated. – Rakesh Soni Dec 03 '14 at 07:52
-
1@RakeshSoni It's deprecated meaning it should not be used. It doesn't mean that it won't work. – Kayaman Dec 03 '14 at 07:53
-
What do you mean by 'kill a thread by throwing exception'? Do you throw an exception within the `run` method of your thread or task that is run by a thread? – isnot2bad Dec 03 '14 at 07:56
-
@Kayaman : Since stop has been deprecated. we should not use it. http://stackoverflow.com/questions/16504140/thread-stop-deprecated. (1) within a tread there is no need to call the Thread.interrupt()... since thread's code can terminate itself by adding some conditional logic. (2) if outsider code(not part of tread code) call the thread.interrupt then thread code can catch the exception... release the resource and can terminate itself. -------- OR best way is to maintain a boolean flag like isRunning.... if other code set this flag to false.... then tread code should terminate itself – Rakesh Soni Dec 03 '14 at 08:12
-
@RakeshSoni I know that we shouldn't use it. But if you intend to kill a thread, you may as well call `stop()` instead of creating essentially your own version of `stop()`, which is what the OP was talking about. – Kayaman Dec 03 '14 at 10:48
2 Answers
The best way to stop a thread is to throw a InterruptedException
from within the thread. If an exception is thrown this makes sure that catch
and finally
blocks are executed, so the thread is able to release locks and resources, close files and connections it has opened, etc.
Often you want to stop a thread from another thread. You should not use the Thread.stop()
method, because it stops the thread immediately, resulting in deadlocks when the closed thread still own locks, files which are never closed, etc. The "official" way to stop a thread is to call the interrupt()
method on the thread you want to stop. This sets an internal flag, which can be checked by Thread.interrupted()
. Each thread should now poll Thread.interrupted()
at convenient times and throw an InterruptedException
if the flag is set:
if (Thread.interrupted()) throw new InterruptedException();

- 12,677
- 8
- 34
- 50
-
From the comments, you should have realized that the OP's problem is exactly that he doesn't have a guarantee that the thread-to-be-stopped ever gets to the point of "convenient times". – Erwin Smout Dec 03 '14 at 08:29
-
@ErwinSmout. Really? I see one single comment by the OP, which 1) was added while I was typing my answer, so I didn't read it before, and 2) does not seem to say what you claim it says, only that he wants the thread to stop "at a precise time". The point is, you cannot *safely* stop a thread at a precise time because you need to give the thread time to clean up, and any good answer should point this out. – Hoopje Dec 03 '14 at 09:00
You should not kill a thread. You should make it stop. If you have a loop, you should stop this loop. If you have some time consuming function, like network connect, you should stop this network function instead of killing thread. There is no guarantee to stop a thread with Thread.stop()
function
-
1Oh `Thread.stop()` will stop a thread just fine (unless the security manager prevents it). It's just not very gentle about it. – Kayaman Dec 03 '14 at 07:53
-
It might be. but function itself is deprecated. it means, you should not use it. Even, the function can be deleted from jre at some point. http://10kloc.wordpress.com/2013/03/03/java-multithreading-steeplechase-stopping-threads/ – Adem Dec 03 '14 at 07:57
-
Exactly, you shouldn't use it because it has problems. However it's unlikely that it would be removed (even though in theory it could be). It would cause more harm than benefit. – Kayaman Dec 03 '14 at 08:00
-
Then what do you do when you need to stop a thread at a precise time? – Jason Ching Dec 03 '14 at 08:11
-
The Thread.stop() is the only way you can do it, if the thread is to be cancelled immediately no matter what. You'd better be prepared for dealing with the consequences of this "no matter what". – Erwin Smout Dec 03 '14 at 08:25