【JAVA学习路-think in java】p161:共享对象时,通过引用计数来清理对象

package pkg;
public class p161 {
	public static void main(String[] args) throws Throwable {
		Shared shared = new Shared();//[1]
		Composing[] composings = new Composing[]{new Composing(shared),new Composing(shared),
new Composing(shared),new Composing(shared),new Composing(shared),new Composing(shared)};
//[3] sharing 'shared' now
		
		for (Composing c:composings){
			c.dispose();//[5]
			c.finalize();//[9][*4]
		}
		System.gc();//
	}
}


//-------1st class
class Shared {
	private int refcount = 0;
	private static long counter = 0;//global counter for shared
	private final long id = counter ++;//the ID  for shared objects from 0,1,2。。。
	
	public Shared(){
		System.out.println("Creating "+this);//[2]
	}
	//******1
	public void addRef(){
		refcount ++;
	}
	//******2
	protected void dispose(){
		if (--refcount == 0){//invoke .dispose() means kill one SHARED object
			System.out.println("::DISPOSED "+this);//[*3]already disposed when you see this
		}
		else
			System.out.println("disposing   "+this);//[8]
	}
	//******3
	@Override
	public String toString() {
		return "Shared id=" + id;
	}
	//******4
	@Override
	protected void finalize() throws Throwable {
		if (--refcount!= 0){
			return;
		} else {
			super.finalize();
		}
	}
	//******5
	public int getRefcount() {
		return refcount;
	}
}

//-------2nd class
class Composing {
	private Shared shared;//just a NULL
	private static long counter = 0;
	private final long id = counter ++;
	
	public Composing(Shared shared){
		System.out.println("Creating " + this);//[4]*6
		this.shared = shared;
		this.shared.addRef();//refcount ++;
	}
	//******1
	protected void dispose(){
		System.out.println("disposing "+this);//[6][*1]
		shared.dispose();//[7][*2]
	}
	//******2
	@Override
	public String toString() {
		return "Composing id=" + id;
	}
	//******3
	@Override
	protected void finalize() throws Throwable {
		if (shared.getRefcount()!= 0){
		System.out.println("can not finalize Composing now, Refcount= "+shared.getRefcount());
			return;
		}
		else {
		System.out.println("finalize Composing now,Refcount= "+shared.getRefcount());//[10][*5]
			super.finalize();
		}
	}
}

OUTPUT:有简化对应的执行流程,标号不代表绝对数值次序

【2】Creating Shared id=0
【4×6】Creating Composing id=0
Creating Composing id=1
Creating Composing id=2
Creating Composing id=3
Creating Composing id=4
Creating Composing id=5
【6】disposing Composing id=0
【8】disposing   Shared id=0
【10】can not finalize Composing now, Refcount= 5
disposing Composing id=1
disposing   Shared id=0
can not finalize Composing now, Refcount= 4
disposing Composing id=2
disposing   Shared id=0
can not finalize Composing now, Refcount= 3
disposing Composing id=3
disposing   Shared id=0
can not finalize Composing now, Refcount= 2
disposing Composing id=4
disposing   Shared id=0
can not finalize Composing now, Refcount= 1
【*1】disposing Composing id=5
【*3】::DISPOSED Shared id=0
【*5】finalize Composing now,Refcount= 0

总结:

1、1个基类对象,可能被多次共享。因此需要引用计数refcount来确保能正确调用dispose()方法。

 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值