A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 徐梦侠 中级黑马   /  2012-10-19 12:44  /  1705 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 徐梦侠 于 2012-10-19 14:52 编辑

如果不含代码stu=new Student("李四");System.out.println(stu.toString());,打印结果就是Finalize Object : Student : 张三。
说明空间已经释放,那应该先打印Finalize Object : Student : 张三,可为什么结果不是这样呢?

class Student{
    private String name;
    public Student(String name){
            this.name = name;
    }
    public String toString(){
            return("Student : " + this.name);
    }
    public void finalize() throws Throwable{
            System.out.println("Finalize Object : " + this);
    }
}
class SystemDemo02{
public static void main(String[] args){
   Student stu = new Student("张三");
   stu = null;                //断开引用
   //如果让线程等待1分钟,即运行一段时间,再运行系统垃圾回收就会调用fianlize方法了
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   System.gc();        //强制释放空间
   stu=new Student("李四");
   System.out.println(stu.toString());
}
}
为什么打印结果会是:
Student : 李四
Finalize Object : Student : 张三

评分

参与人数 1技术分 +1 收起 理由
刘芮铭 + 1 赞一个!

查看全部评分

3 个回复

倒序浏览
上面的程序如果多试验几次就会发现,两条语句的打印顺序是随机的。

出现这种情况的原因是,程序在调用System.gc();语句时,虚拟机会根据需要在单独的线程中自动执行回收过程。

也就是说,主线程负责打印"Student : 李四",而用来回收垃圾的线程负责打印"Finalize Object : Student : 张三"。

两个线程抢占CPU是随机的,所以两条语句的打印顺序也是随机的。

评分

参与人数 1技术分 +1 收起 理由
韩军博 + 1 很给力!

查看全部评分

回复 使用道具 举报
赵云柯 发表于 2012-10-19 14:37
上面的程序如果多试验几次就会发现,两条语句的打印顺序是随机的。

出现这种情况的原因是,程序在调用Syst ...

受教了,应该就是用的单独的线程执行回收过程
回复 使用道具 举报
class Student{
    private String name;
    public Student(String name){
            this.name = name;
    }
    public String toString(){
            return("Student : " + this.name);
    }
    public void finalize() throws Throwable{
            System.out.println("Finalize Object : " + this);
    }
}
class SystemDemo02{
public static void main(String[] args){
   Student stu = new Student("张三");
   stu = null;                //断开引用
  
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   System.gc();       //这也不是强制启动垃圾回收,只是显式的启动了GC垃圾回收,        
                              //但是jvm虚拟机并不会立即去执行,也可能会立即执行,所以输出结果不一样。   
    stu=new Student("李四");
   System.out.println(stu.toString());
}
}

评分

参与人数 1技术分 +1 收起 理由
韩军博 + 1 很给力!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马