恳求引用以下代码,我只想做一些关于不安全的事情。
import sun.misc.Unsafe;
import java.lang.reflect.Field;
import java.util.*;
public class A {
public static void main(String[] args) throws Exception {
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
Unsafe u = (Unsafe) f.get(null);
System.out.println("the while start at:" + new Date());
long total = 0;
while (true) {
u.allocateInstance(B.class);
total++;
if (total % 100000000 == 0) {
System.out.println(total);
System.gc();
}
}
}
}
class B {
private int a;
private int b;
private double d;
private float e;
@Override
protected void finalize() {
try {
super.finalize();
} catch (Throwable e) {
System.out.println("catch excep");
}
System.out.println("class B finalize, the a:" + a);
}
}
代码永远不会被调用,但B类的终结永远不会被调用。为什么?我找不到关键信息....
凤凰求蛊
相关分类