Your method stops in a loop (instead of ending it) because the remote shell does not close the output stream. There is no reason for this, since there you can send more commands.
If you want to execute only one command (or a series of previously known commands), you should not use the Shell channel, but the "exec" channel.
Thus, the remote shell (which executes your command) will complete the execution of your command, and then the server will close the stream. That way, your loop will end, and then you can close the threads.
If you think you need a shell channel (for example, if you need to run several commands in one context and respond to one output before deciding what will be next), you will need some way to know when one command completed (for example, by recognizing an invitation), and then send the next one. To complete the work, either close the output stream, or send the command "logout" or "log off" (for any standard unix shell or for other shells different commands may be required), then the remote site must also close another stream.
By the way, when disabling strict verification of the host key, this also opens your connection with a man in the middle attack, and in case of password authentication, an attacker can seize your password. The right way to do this is to configure a properly initialized host key repository to recognize the remote host key.
source share