Say that (possible on a separate thread) I am running some method ClassA.foobar(). Inside that method is a try, (possibly catch), finally block.
Now if the last reference to this ClassA object (or to this thread) is lost when execution is still in the middle of the try-block (or the catch-block), could this object(/thread) get garbage collected before I get to the finally block? In other words: is the finally block still guaranteed to run even if there are no strong references to the object(/thread) left in memory?
(I don't know how the GC handles orphaned live threads.)
Dummy example:
[Some context]
{
ClassA classA = new ClassA();
//Point is this instance and a reference to it exists
class ClassA
{
public void foobar()
{
try
{
classA = null;
//Point is that the last reference to this instance is lost,
//and that this happens at this point in foobar() execution.
//The actual location of this line of code is irrelevant.
}
finally
{
//some important stuff here!
}
}
}
}