I have seen many Thread java examples and Runnable objects are created as tasks and passed to thread.
As there is no reference to these tasks, so why is this task not garbage collected by java?
Or is garbage collected and I am asking the wrong question here?
Please share your valuable thoughts.
I have seen many Thread java examples and Runnable objects are created as tasks
Share
The fact that you don’t have an explicit reference to an object doesn’t mean that an internal JVM object doesn’t hold one to it.
Take an example:
There is no reference to it from a developer point of view but internally the frame has a list of components. This is what happen with threads, the internal scheduler must keep a reference to them for sure.
The thread itself will be garbage collected just when released from the scheduler (so that no reference effectively exists to it anymore)