Java multithreading: behavior when the number of references to a stream object becomes zero

[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(){ //a heavy function } } 

And now in the main thread, I call this thread as:

 public static void main(...){ new MyThread().start(); System.gc(); //just a request.. //end of main thread too. //Reference to that MyThread object is now zero. } 

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 :)

+4
source share
3 answers

Each running thread represents a root for the GC. Any object accessible from one of the roots is not suitable for GC, and the thread has a local thread reference to the java.lang.Thread instance ( Thread.currentThread() returns). So no, your thread will not be GCed until it ends, since the Thread instance is available from the current thread.

I don’t understand why it would be nice not to call join() on the generated thread. If you do not care when the spawned thread ends, you do not need to join it. The application will stop when the last non-daemon thread stops working.

Also note that the number of links is not what the GC uses to determine if an object is suitable for the GC or not. Graphs of objects that support links to each other (for example, the DOM tree) may be eligible for GC if the graph is no longer available.

+13
source

1. JVM will terminate only when all the Non-Daemon thread including the Main thread completed. (The main thread is not the main () method).

2. A Thread will die immediately, as it has finished running the run () method . But since you will know that each thread (toe, i.e. thread of execution) is associated with an instance of the Thread class.

3. So, when the thread dies, it goes into a dead state (I do not mention the thread pool here), but an object of the Thread class that was associated with the thread still exists, but lost its thread forever.

4. But there is a high probability that your thread still works only upon completion of the main method.

5. The call to join() is not bad at all, but it should be used with caution.

+4
source

When the reference count is zero, will the thread match the GC?

Not. The stream becomes suitable for the GC when it terminates, and there are no links.

If this is really acceptable, what is the behavior of garbage collection? Will the stream open?

See above.

I know this is bad, but is it good if you don't have otherThread.join () in main ()?

Not bad, and it’s completely correct: the JVM will exit when all non-daemon threads exit.

JVM maitains refers to a thread while it is active. So the reference count is never zero.

Right.

The executing function implies this link, so the reference count is again non-zero.

Wrong. Consider static methods. The executing function is executed in the active stream by definition, therefore the stream is by definition active, therefore it cannot be GC'd. Your thinking here is pretty circular.

+2
source

All Articles