Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:请问我是错了还是答案错了 [Re:aleel_008]
arthas1982





发贴: 11
积分: 0
于 2004-10-09 09:47 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
只有在当前线程消亡了时候,此线程的类变量才被JVM收集。dorrenchen仁兄的功底好深啊,小弟一时没有看懂。小弟写了个自己的,大家看看
import java.lang.ref.WeakReference;

public class Garbage{
public Object a;
public Object b;
public Object c;
public WeakReference aR; //WeakReference 不会影响JVM对他所引用的对象的收集
public WeakReference bR;
public WeakReference cR;

public Garbage(){
a = new Object();
b = new Object();
c = new Object();
aR = new WeakReference(a);
bR = new WeakReference(b);
cR = new WeakReference(c);
}

public void run(){
System.out.println("run() Begin!");
print();
a = b;
System.out.println("execute a = b");
print();
a = c;
System.out.println("execute a = c");
print();
c = null;
System.out.println("execute c = null");
print();
a = null;
System.out.println("execute a = null");
print();
System.out.println("run() End!");
}

public void print(){
System.out.println("a :"+aR.get());//如果a:后是null,则说明他被回收了
System.out.println("b :"+bR.get());
System.out.println("c :"+cR.get());
}
}
public class Test {

  public static void main(String[] args) {
    Garbage g = new Garbage();
    g.run();
  }
}




话题树型展开
人气 标题 作者 字数 发贴时间
4574 请问我是错了还是答案错了 aleel_008 282 2004-10-06 19:44
3557 Re:请问我是错了还是答案错了 COWCOW 5 2004-10-06 19:49
3801 Re:请问我是错了还是答案错了 aleel_008 5 2004-10-06 23:33
3755 Re:请问我是错了还是答案错了 dorrenchen 803 2004-10-06 23:48
3725 Re:请问我是错了还是答案错了 aleel_008 361 2004-10-07 03:01
3704 Re:请问我是错了还是答案错了 dorrenchen 1533 2004-10-07 05:39
3668 Re:请问我是错了还是答案错了 aleel_008 119 2004-10-07 15:17
3930 Re:请问我是错了还是答案错了 arthas1982 1279 2004-10-09 09:47

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923