隔离岛上的垃圾收集
Garbage Collection on Island of Isolation
根据我的理解,任何没有引用的对象都可以进行垃圾收集,即在收集垃圾时应调用 class 的 finalize 方法。我在下面有一个 class 和一个问题,为什么在主 class 中没有引用的线程对象在垃圾收集时不调用 finalize 方法。
package com;
class Customer {
int amount = 10000;
synchronized void withdraw(int amount) {
System.out.println("going to withdraw...");
if (this.amount < amount) {
System.out.println("Less balance; waiting for deposit...");
try {
wait(10000);
} catch (Exception e) {
}
}
this.amount -= amount;
System.out.println("withdraw completed...");
}
synchronized void deposit(int amount) {
System.out.println("going to deposit...");
this.amount += amount;
System.out.println("deposit completed... ");
notify();
}
}
class Test {
public static void main(String args[]) throws InstantiationException, IllegalAccessException {
final Customer c = new Customer();
//Island of Isolating a Thread
new Thread() {
public void run() {
// System.out.println("withdraw thread ");
c.withdraw(15000);
}
}.start();
//Island of Isolating another Thread
new Thread() {
public void run() {
// System.out.println("deposit thread ");
c.deposit(10000);
}
}.start();
//attempting to gc manually.
System.gc();
}
//Calling a finialize() method to check whether it is garbage collected or not
protected void finalize() throws Throwable {
System.out.println("Finalize method called");
}
}
编辑:new Thread() {//blah blah }.start();是获取创建堆的非引用对象。即它没有堆栈引用。 理论上,任何非堆栈引用对象都有资格进行垃圾收集,实际上适用于垃圾收集。由于它们不是堆栈引用,因此也被视为隔离岛。
请问是不是我的理解有误。谢谢。如果我的想法自相矛盾,请发表你的看法。
why the thread objects with no reference in the main class are not calling finalize
垃圾收集器将活动线程视为活动线程,即使没有其他线程引用这些线程。
换句话说,垃圾收集不会使 运行 代码突然终止。真会让人头疼。
最重要的是,您只在从未实例化的 class 上实现了 finalize
。
根据我的理解,任何没有引用的对象都可以进行垃圾收集,即在收集垃圾时应调用 class 的 finalize 方法。我在下面有一个 class 和一个问题,为什么在主 class 中没有引用的线程对象在垃圾收集时不调用 finalize 方法。
package com;
class Customer {
int amount = 10000;
synchronized void withdraw(int amount) {
System.out.println("going to withdraw...");
if (this.amount < amount) {
System.out.println("Less balance; waiting for deposit...");
try {
wait(10000);
} catch (Exception e) {
}
}
this.amount -= amount;
System.out.println("withdraw completed...");
}
synchronized void deposit(int amount) {
System.out.println("going to deposit...");
this.amount += amount;
System.out.println("deposit completed... ");
notify();
}
}
class Test {
public static void main(String args[]) throws InstantiationException, IllegalAccessException {
final Customer c = new Customer();
//Island of Isolating a Thread
new Thread() {
public void run() {
// System.out.println("withdraw thread ");
c.withdraw(15000);
}
}.start();
//Island of Isolating another Thread
new Thread() {
public void run() {
// System.out.println("deposit thread ");
c.deposit(10000);
}
}.start();
//attempting to gc manually.
System.gc();
}
//Calling a finialize() method to check whether it is garbage collected or not
protected void finalize() throws Throwable {
System.out.println("Finalize method called");
}
}
编辑:new Thread() {//blah blah }.start();是获取创建堆的非引用对象。即它没有堆栈引用。 理论上,任何非堆栈引用对象都有资格进行垃圾收集,实际上适用于垃圾收集。由于它们不是堆栈引用,因此也被视为隔离岛。
请问是不是我的理解有误。谢谢。如果我的想法自相矛盾,请发表你的看法。
why the thread objects with no reference in the main class are not calling finalize
垃圾收集器将活动线程视为活动线程,即使没有其他线程引用这些线程。
换句话说,垃圾收集不会使 运行 代码突然终止。真会让人头疼。
最重要的是,您只在从未实例化的 class 上实现了 finalize
。