The string will not be garbage collected until the thread is set to null or some other thread , because there is a chain of links leading to the object from the static variable.
The link chain looks like this:
static thread implicitly refers to the instance of Foo from which it was created through an instance of an anonymous class derived from Runnable . In turn, the Foo instance contains a reference to the thing , making sure that the object does not receive garbage collection.
What if the instance of Foo is long gone?
Foo not going anywhere, because it is stored live by implicit link from the thread object.
Note. . This answer intentionally ignores the effect of interning String objects created from string literals.
source share