Are there multiple threads still existing after the message "I'm in the thread"? Or every time the start function is completed, the thread is destroyed?
In your case, you create a lot of threads, because a stream is created for each button click.
Thread lifespan is completed after the last statement in the run() method has completed. After the run() method is executed, the thread will enter the TERMINATED State , and it cannot be restarted.
A better solution does not create a new thread for every button click. If you need more threads in your application, go to the thread pool.
Java provides the Executor framework for this purpose. It better manages the life cycle of threads.
Use one of the APIs that will return the ExecutorService from Executors
eg. newFixedThreadPool(4)
Take a look at this post and this article for more options.
If I create multiple threads that work simultaneously, how can I close them my own way?
You can close the ExecutorService as indicated below in the SE message:
How to close java ExecutorService correctly
Since you are using Android, you have another good alternative to multithreading: HandlerThread and Handler
See below for more details.
Android: toast in the stream
source share