Life of threads in java

Asked

Viewed 113 times

3

When an object, from a class that implements a thread, is destroyed (loses reference) the thread referring to that object will stop?

1 answer

4


According to the Java documentation in System.Threading.Thread:

It is not necessary to retain a Reference to a Thread Object Once you have Started the thread. The thread continues to execute until the thread Procedure is complete.

That is, it is not necessary to keep a reference to a Thread after starting it, the JVM manages the Thread and only terminates its use when run() runs until its end.

Although it is interesting to keep the reference for Thread control purposes.

I hope I’ve helped.

  • Excellent! It did help! I’ll ask you another question, if you don’t mind: But if the object loses the reference, will Garbage Ollector wipe this object from memory? And if it clears, how are the elements used by the thread (class attributes and methods)???

  • That’s exactly it, the GC will keep the internal reference (and therefore these objects in memory) until the thread usage is completed. Done this both the thread and the objects used by it lose "scope" and are released from memory.

  • It makes a lot of sense. I will search for these documentations for more details. But you have already clarified my doubts. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.