Say I spawn a thread every couple seconds using the method below and every thread takes about a second to complete. Do the finished threads get deleted?
new Thread (new myRunnableClass()).start();
The native, OS-level thread is released as soon as the thread finishes (circa when run()
finishes), but the thread object lives, like any other object, until it becomes unreachable and the garbage collector feels like running.
Edit: It might also be interesting to know that Thread
(in Sun's Oracle's implementation, anywho) has a private method called by the VM when the thread exits, which aggressively nulls several fields, including the one referring to the Runnable
set by the Thread(Runnable)
constructor. So even if you retain a reference to the Thread
, the things it doesn't need after finishing execution will be released, regardless.
I would not call it deleted. Once the thread completes, it will go to dead state getting ready to be garbage collected by the JVM.
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