What should be done with a Thread after its run()
method finishes executing? Is there any cleanup needed for a Thread in Java?
We have found that just washing them or giving them a scrub with pipe cleaners is not sufficient to remove things like rust, burnt oil and grime. The only way to really clean the threads is to run a thread chaser or very carefully a thread cutting tap.
Once a thread in the thread pool completes its task, it's returned to a queue of waiting threads. From this moment it can be reused. This reuse enables applications to avoid the cost of creating a new thread for each task. There is only one thread pool per process.
Java Thread destroy() methodThe destroy() method of thread class is used to destroy the thread group and all of its subgroups. The thread group must be empty, indicating that all threads that had been in the thread group have since stopped.
The Terminated property is set from a different thread to signal to the worker thread that it should terminate. It's then up to the worker thread to obey the signal by checking the Terminated flag in the Execute procedure. After the Execute procedure is finished, the Thread's Finished property is automatically set.
Unless the thread's work has used some unmanaged resources (network streams, files etc) - in which case it should clean up after itself - there's nothing you need to do.
Note that holding a reference to the Thread
object representing the thread won't keep the underlying OS thread alive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With