Itβs not easy to just kill the thread because the language developers want to avoid the following problem: your thread takes the lock, and then you kill it before it can release it ... now anyone who needs this lock will be stuck.
What you need to do is use some global variable to stop the thread. You must manually, in your stream code, check that the global variable and return, if you see, that means you have to stop.
source share