How can I terminate Thread immediately? (Don't interrupt)

It is not a question of how to cleanly end the stream, that is, by calling an interrupt on it and the corresponding response of the stream. I cannot change the code that the thread executes.

I specifically want to immediately stop Thread, I do not care that the state of things remains. I know that something like this is possible using Thread.stop, but actually it throws a ThreadDeath exception, and for Thread to it is impossible to stop this exception. However, the code that I mean causes this exception and does not reconstruct it.

Thread.destroy () seemed to be what I was looking for, however this method was never implemented. Is there any other way to achieve this?

+6
source share
4 answers

I believe that in Java there is no way to just kill a thread, as you describe. As you noted in the comment, interrupt will not do what you want. If the thread executes, it simply sets a flag, and it notices this before the thread. if the thread is waiting or sleeping, it will throw an InterruptedException .

The only way I can imagine doing what you are describing is to kill the process in which the thread is running. (For example, calling System.exit(int) .)

+3
source

There is no way. From Java Concurrency in Practice :

Since there is no proactive way to stop the flow, they should instead be convinced to disconnect on their own.

Interrupting a thread is not a cleaner way, as you said. Pure methods can be:

  • ExecutorService.shutdown ()
  • Future.cancel ()
  • Poison pills

You should not send jobs to threads that take time. You would divide them into smaller tasks and send poison pill to cancel a large task. If you do not, then spawn/fork a process and kill it if you want to cancel the task.

+2
source

If you do not trust the thread in question to the extent that you need to kill it, you probably would be better off running it in a separate process and killing the process.

In any case, the following code may work if you are fine with obsolete Thread methods:

  while (theThread.isAlive()) { theThread.stop(); } 

Depending on how hard the stream is trying to survive ...

You might want to run this code in multiple threads or repeat the stop() call if that is not enough. However, I managed to kill the following thread with this code:

  final Thread iWontDie = new Thread(() -> { int i = 0; while (true) { try { System.out.println("I'm still alive! " + ++i); } catch (Throwable t) { // eat t } } }); iWontDie.start(); 
+1
source

If you are using Java 7 or earlier, you can use the overloaded stop method (Throwable obj) to throw something other than ThreadDeath error:

Causes the thread to stop executing. If obj is null , a NullPointerException thrown (in the current thread). The thread represented by this thread is forced to stop everything that it does abnormally and throw a Throwable obj object as an exception. This is an unusual action; A stop method that takes no arguments is usually used.

This method, like the parameterless version, is deprecated, so just keep that in mind.

0
source

All Articles