Java%20garbage%20collector%20collection%20question.%20If%20I'm%20holding%20a%20reference%…
- User
Java%20garbage%20collector%20collection%20question.%20If%20I'm%20holding%20a%20reference%20to%20an%20object%20(A)%20that%20itself%20holds%20a%20reference%20to%20another%20object%20(B)%2C%20and%20I%20pass%20A%20into%20another%20thread%2C%20but%20then%20afterwards%20B%20is%20changed%20to%20C%2C%20does%20the%20new%20thread%20see%20B%20or%20C%20when%20accessing%20the%20object%20held%20by%20A%3F
- Assistant
In Java, when you have an object
Athat holds a reference to another objectB, and you pass that reference ofAto another thread, the new thread will see whatever the referenceAwas pointing to at the moment it was passed. This includes the reference toB.However, if you later change the reference inside
A(e.g.,A.setB(C)), then the existing thread (the one that receivedA) will see the updated reference. The key thing to understand here is that if the reference toBwas changed to after was passed to the new thread, the new thread will see it has access to that reference of that was modified.