说(可能在单独的线程上)我正在运行一些方法 ClassA.foobar()。该方法内部是一个 try, (可能是 catch), finally 块。
现在,如果在执行仍在 try 块(或 catch 块)中间时丢失了对这个 ClassA 对象(或这个线程)的最后引用,那么这个对象(/线程)是否可以在我得到之前得到垃圾收集到finally块?换句话说:即使内存中没有对对象(/线程)的强引用,finally 块是否仍能保证运行?
(我不知道 GC 如何处理孤立的活动线程。)
虚拟示例:
[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!
}
}
}
}