// polymorphism/ReferenceCounting13.java
// TIJ4 Chapter Polymorphism, Exercise 13, page 300
/* Add a finalize() method to ReferenceCounting.java to verify the termination
* condition (see the Initialization & Cleanup chapter).
*/
//import static net.mindview.util.System.out.println.*;class Shared {
private int refcount = 0;
private static long counter = 0;
private final long id = counter++;
public Shared() {
System.out.println("Creating " + this);
}
public void addRef() { refcount++; }
protected void finalize() {
if(refcount > 0)
System.out.println("Error: " + refcount + " Shared " + id + " objects in use");
}
protected void dispose() {
if(--refcount == 0)
System.out.println("Disposing " + this);
}
public String toString() { return "Shared " + id; }
}class Composing {
private Shared shared;
private static long counter = 0;
private final long id = counter++;
public Composing(Shared shared) {
System.out.println("Creating " + this);
this.shared = shared;
this.shared.addRef();
}
protected void dispose() {
System.out.println("Disposing " + this);
shared.dispose();
}
public String toString() { return "Composing " + id; }
}public class ReferenceCounting13 {
public static void main(String[] args) {
Shared shared = new Shared();
Composing[] composing = { new Composing(shared),
new Composing(shared), new Composing(shared),
new Composing(shared), new Composing(shared)
};
for(Composing c : composing)
c.dispose();
Composing compTest = new Composing(shared);
Composing compTest2 = new Composing(shared);
// Test finalize():
shared.finalize();
Shared sharedTest = new Shared();
Composing compTest3 = new Composing(sharedTest);
// Test sharedTest finalize():
sharedTest.finalize();
}
}
在 c.dispose()之后,shared应该已经给dispose掉了,为什么后面却依然可以 new Composing(shared)???? for(Composing c : composing)
c.dispose();
Composing compTest = new Composing(shared);
Composing compTest2 = new Composing(shared);
这是书中的源程序
// TIJ4 Chapter Polymorphism, Exercise 13, page 300
/* Add a finalize() method to ReferenceCounting.java to verify the termination
* condition (see the Initialization & Cleanup chapter).
*/
//import static net.mindview.util.System.out.println.*;class Shared {
private int refcount = 0;
private static long counter = 0;
private final long id = counter++;
public Shared() {
System.out.println("Creating " + this);
}
public void addRef() { refcount++; }
protected void finalize() {
if(refcount > 0)
System.out.println("Error: " + refcount + " Shared " + id + " objects in use");
}
protected void dispose() {
if(--refcount == 0)
System.out.println("Disposing " + this);
}
public String toString() { return "Shared " + id; }
}class Composing {
private Shared shared;
private static long counter = 0;
private final long id = counter++;
public Composing(Shared shared) {
System.out.println("Creating " + this);
this.shared = shared;
this.shared.addRef();
}
protected void dispose() {
System.out.println("Disposing " + this);
shared.dispose();
}
public String toString() { return "Composing " + id; }
}public class ReferenceCounting13 {
public static void main(String[] args) {
Shared shared = new Shared();
Composing[] composing = { new Composing(shared),
new Composing(shared), new Composing(shared),
new Composing(shared), new Composing(shared)
};
for(Composing c : composing)
c.dispose();
Composing compTest = new Composing(shared);
Composing compTest2 = new Composing(shared);
// Test finalize():
shared.finalize();
Shared sharedTest = new Shared();
Composing compTest3 = new Composing(sharedTest);
// Test sharedTest finalize():
sharedTest.finalize();
}
}
在 c.dispose()之后,shared应该已经给dispose掉了,为什么后面却依然可以 new Composing(shared)???? for(Composing c : composing)
c.dispose();
Composing compTest = new Composing(shared);
Composing compTest2 = new Composing(shared);
这是书中的源程序
1、将代码进行良好的格式化,以方便阅读。
2、在发帖文本框的上方单击“#”按钮,选择 Java
3、将代码粘贴到【code=Java】和【/code】之间。发出来的帖子就会是下面的效果:public class Hello { // 程序入口
public static void main(String[] args) {
System.out.println("Hello!");
}
}
java中对象无法被控制回收 只能通过垃圾收集器回收