引用计数 java_JAVA中使用引用计数技术来实现垃圾回收机制

package com.test11;

/**

* 测试对象的销毁

* @author Administrator

*

*/

public class Apple{

/**

* 属性

*/

private int refCount = 0;//某个引用被使用的次数

private static long counter = 0;//引用的个数

private final long id = counter++;//引用的id

/**

* 构造器

*/

public Apple(){

System.out.println("买了个苹果\t apple"+id);

}

/**

* 某个引用,每被使用一次引用次数加一

*/

public void addRef(){

refCount++;

}

/**

* 销毁对象

*/

protected void dispose(){

if(--refCount==0){

System.out.println("所有人吃完了苹果apple"+id+",现在丢掉苹果核!");

counter--;

}else{

System.out.println("其他人还没吃完苹果,苹果先不销毁!");

}

}

/**

* get方法

*/

public int getRefCount() {

return refCount;

}

public static long getCounter() {

return counter;

}

public long getId() {

return id;

}

}

----------------------------------------------------------------

package com.test11;

/**

* 测试对象的销毁

* @author Administrator

*

*/

public class Person{

/**

* 属性

*/

private Apple apple;

private static long counter = 0;//引用的个数

private final long id = counter++;//引用的id

private String name;

/**

* 构造器

*/

public Person(Apple apple,String name){

this.apple = apple;

this.name = name;

System.out.println(name+"走进了小屋中!");

this.apple.addRef();

}

/**

* 销毁对象

*/

protected void dispose(){

System.out.println(name+"走出了小屋!");

apple.dispose();

}

/**

* 吃苹果

*/

protected void eatApple(){

System.out.println(name+"吃了一口苹果!");

}

/**

* get方法

*/

//public int getApple() {

//return apple;

//}

public long getCounter() {

return counter;

}

public long getId() {

return id;

}

}

----------------------------------------------------------------

package com.test11;

/**

* 使用引用计数技术对对象的销毁

*

* @author Administrator

*

*/

public class Test {

public static void main(String[] args) {

/**

* 现在要在一个小屋内(内存)给人(对象)发苹果(对象)吃!

*/

// 先创建两个苹果

Apple apple0 = new Apple();

Apple apple1 = new Apple();

System.out.println("**********************************************************");

System.out.println("现在的苹果数量为:"+Apple.getCounter());

System.out.println("苹果apple0被引用的数量为:"+apple0.getRefCount());

System.out.println("苹果apple1被引用的数量为:"+apple1.getRefCount());

System.out.println("**********************************************************");

// 创建一群人(五个人),将苹果apple0发给这群人吃(每个人只能吃一口)

Person[] persons = { new Person(apple0,"张三"), new Person(apple0,"李四"),

new Person(apple0,"王五"), new Person(apple0,"赵六"), new Person(apple0,"刘齐") };

System.out.println("**********************************************************");

System.out.println("现在的苹果数量为:"+Apple.getCounter());

System.out.println("苹果apple0被引用的数量为:"+apple0.getRefCount());

System.out.println("苹果apple1被引用的数量为:"+apple1.getRefCount());

System.out.println("**********************************************************");

for(Person p:persons){

p.eatApple();

p.dispose();//吃完苹果的人,要从小屋中出去(销毁对象释放内存)

}

System.out.println("**********************************************************");

System.out.println("现在的苹果数量为:"+Apple.getCounter());

System.out.println("苹果apple0被引用的数量为:"+apple0.getRefCount());

System.out.println("苹果apple1被引用的数量为:"+apple1.getRefCount());

System.out.println("**********************************************************");

}

}

=====================运行结果如下=========================

买了个苹果 apple0

买了个苹果 apple1

**********************************************************

现在的苹果数量为:2

苹果apple0被引用的数量为:0

苹果apple1被引用的数量为:0

**********************************************************

张三走进了小屋中!

李四走进了小屋中!

王五走进了小屋中!

赵六走进了小屋中!

刘齐走进了小屋中!

**********************************************************

现在的苹果数量为:2

苹果apple0被引用的数量为:5

苹果apple1被引用的数量为:0

**********************************************************

张三吃了一口苹果!

张三走出了小屋!

其他人还没吃完苹果,苹果先不销毁!

李四吃了一口苹果!

李四走出了小屋!

其他人还没吃完苹果,苹果先不销毁!

王五吃了一口苹果!

王五走出了小屋!

其他人还没吃完苹果,苹果先不销毁!

赵六吃了一口苹果!

赵六走出了小屋!

其他人还没吃完苹果,苹果先不销毁!

刘齐吃了一口苹果!

刘齐走出了小屋!

所有人吃完了苹果apple0,现在丢掉苹果核!

**********************************************************

现在的苹果数量为:1

苹果apple0被引用的数量为:0

苹果apple1被引用的数量为:0

**********************************************************

1

3

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2015-01-21 15:05

浏览 2705

评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值