13. JVM详解之:本地变量的生命周期
最后更新于
这有帮助吗?
最后更新于
这有帮助吗?
这有帮助吗?
public static class Test {
public Test() {
System.out.println("创建对象 " + this);
}
public void test() {
System.out.println("测试对象 " + this);
}
@Override
protected void finalize() throws Throwable {
System.out.println("回收对象 " + this);
}
}
public static void main(String[] args) throws InterruptedException {
System.out.println("开始测试1");
resetFlag();
flag = true;
testLocalVariable();
System.out.println("等待Test1结束");
Thread.sleep(10000);
System.out.println("开始测试2");
flag = true;
testLocalVariable();
} public static void testLocalVariable() {
Test test1 = new Test();
Test test2 = new Test();
while (flag) {
// 啥都不做
}
test1.test();
}开始测试1
创建对象 com.flydean.LocalVariableReachability$Test@119d7047
创建对象 com.flydean.LocalVariableReachability$Test@776ec8df
回收对象 com.flydean.LocalVariableReachability$Test@776ec8df
测试对象 com.flydean.LocalVariableReachability$Test@119d7047
等待Test1结束
回收对象 com.flydean.LocalVariableReachability$Test@119d7047
开始测试2
创建对象 com.flydean.LocalVariableReachability$Test@4eec7777
创建对象 com.flydean.LocalVariableReachability$Test@3b07d329
回收对象 com.flydean.LocalVariableReachability$Test@3b07d329