只有垃圾回收到你new的对象时finalize方法才被调
试试这个:
public final class Finalize {
public static int count = 0;
private long come;
private long go;
public Finalize() {
come = System.currentTimeMillis();
count++;
System.out.println("\r\nFinalize instance[" + hashCode() + "] borned.");
} protected final void finalize() throws Throwable {
super.finalize();
go = System.currentTimeMillis();
System.out.println("\r\nFinalize instance[" + hashCode() + "] died.");
System.out.println("\r\nLife time: " + (go - come)/1000 + " second(s).");
Thread finalizer = Thread.currentThread();
System.out.println("\r\nFinalizer Thread=[" + finalizer.getClass().getName() + "@" + Integer.toHexString(finalizer.hashCode()) + "].");
count--;
throw new Exception("Hihi.");
} public static void main(String[] args) {
Thread t = new Thread(){
public void run(){
Thread tt = new Thread(){
public void run() {
Finalize f = new Finalize();
}
};
tt.start();
}
};
t.start();
long begin = System.currentTimeMillis();
while(t.isAlive()) {
try {
Thread.currentThread().sleep(1000);
} catch (Exception e) {
}
long end = System.currentTimeMillis();
int time = (int)((end - begin) / 1000);
System.out.print("\r" + time + " second(s) gone.");
}
while(Finalize.count > 0) {
try {
Thread.currentThread().sleep(1000);
} catch (Exception e) {
}
long end = System.currentTimeMillis();
int time = (int)((end - begin) / 1000);
System.out.print("\r" + time + " second(s) gone.");
if (time > 10) {
System.gc();
break;
}
}
}
}
关于垃圾收集有很深的理论,什么样的对象才能拿去销毁有严格的定义
我只知道当线程结束时,线程的本地对象可以拿去销毁
上边例子可以验证
试试这个:
public final class Finalize {
public static int count = 0;
private long come;
private long go;
public Finalize() {
come = System.currentTimeMillis();
count++;
System.out.println("\r\nFinalize instance[" + hashCode() + "] borned.");
} protected final void finalize() throws Throwable {
super.finalize();
go = System.currentTimeMillis();
System.out.println("\r\nFinalize instance[" + hashCode() + "] died.");
System.out.println("\r\nLife time: " + (go - come)/1000 + " second(s).");
Thread finalizer = Thread.currentThread();
System.out.println("\r\nFinalizer Thread=[" + finalizer.getClass().getName() + "@" + Integer.toHexString(finalizer.hashCode()) + "].");
count--;
throw new Exception("Hihi.");
} public static void main(String[] args) {
Thread t = new Thread(){
public void run(){
Thread tt = new Thread(){
public void run() {
Finalize f = new Finalize();
}
};
tt.start();
}
};
t.start();
long begin = System.currentTimeMillis();
while(t.isAlive()) {
try {
Thread.currentThread().sleep(1000);
} catch (Exception e) {
}
long end = System.currentTimeMillis();
int time = (int)((end - begin) / 1000);
System.out.print("\r" + time + " second(s) gone.");
}
while(Finalize.count > 0) {
try {
Thread.currentThread().sleep(1000);
} catch (Exception e) {
}
long end = System.currentTimeMillis();
int time = (int)((end - begin) / 1000);
System.out.print("\r" + time + " second(s) gone.");
if (time > 10) {
System.gc();
break;
}
}
}
}
关于垃圾收集有很深的理论,什么样的对象才能拿去销毁有严格的定义
我只知道当线程结束时,线程的本地对象可以拿去销毁
上边例子可以验证
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货