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()方法。