[Before starting, I tried to find similar questions, since I did not find a single question, I ask a question here]
I am learning Java, and the following script hit my head:
class MyThread extends Thread { void run(){
And now in the main thread, I call this thread as:
public static void main(...){ new MyThread().start(); System.gc();
I ran this code. It seems the stream is still alive. The program ends when all threads are disabled.
My questions:
- When the reference count is zero, will the thread match the GC? If this is really acceptable, what is the behavior of garbage collection? Will the flow be stopped?
- I know this is bad, but is it clear that it does not have
otherThread.join() in main() ?
I have several explanations for myself (but I donβt know how I am right - the reason I made the message here):
- JVM maitains refers to a thread if it is active . Thus, the reference count is never zero.
- The execution function has an implicit
this link, so the reference count is again non-zero.
Am I correct in any of the explanations above? Or is there any other explanation?
Thank you and welcome :)
source share