PDA

View Full Version : Thread Garbage Collection



shashanksingh
March 7th, 2012, 12:12 PM
I have a question in java.

Suppose a thread is in exucution and suppose it still ahs a long time before it finishes it job. However its reference is lost in the 'main' thread.

Will the thread be available for garbage collection once its reference is lost or only after it has finished executing?

Some Penguin
March 7th, 2012, 12:16 PM
Running threads are valid roots from the perspective of garbage collection, and thus are the contents are considered 'live'.

shashanksingh
March 14th, 2012, 06:03 AM
So suppose my program made a thread somewhere and say it forgot to stop it before teminating.
So after my application has terminated, how long would the other thread be running around in the system ? Will it continue to exist untill the JVM is shut down?

I know its bad programming practice to do so, but just in case I lost its reference somewhere?

Some Penguin
March 14th, 2012, 06:50 AM
The JVM will shut down when all non-daemon threads have terminated, or when you explicitly kill it (e.g. System.exit(someintegerstatuscode)). At that point, any daemon thread will be terminated.

If you start a thread but do NOT mark it as a daemon thread, and don't explicitly shut down the JVM, then it won't shut down until that thread exits.