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();
source share