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();
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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(inSun’sOracle’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 theRunnableset by theThread(Runnable)constructor. So even if you retain a reference to theThread, the things it doesn’t need after finishing execution will be released, regardless.