当前位置: 技术问答>java相关
finalized()问题?
来源: 互联网 发布时间:2015-06-13
本文导语: 我在thinking in java 中看到这么一段程序,不明白它的执行顺序是什么样的。 class Chair{ static boolean gcrun=false; static boolean f=false; static int created=0; static int finalized=0; int i; Chair(){ i=++created; ...
我在thinking in java 中看到这么一段程序,不明白它的执行顺序是什么样的。
class Chair{
static boolean gcrun=false;
static boolean f=false;
static int created=0;
static int finalized=0;
int i;
Chair(){
i=++created;
if(created==47)
System.out.println("Created 47");
}
protected void finalize(){
if(!gcrun){
gcrun=true;
System.out.println("Beginning to finalize after"+created +"Chairs have been created");
}
if(i==47){
System.out.println("Finalizing Chair #47,"+"Setting flag to stop Chair creation");
f=true;
}
finalized++;
if(finalized>=created)
System.out.println("All"+finalized+"finalized");
}
}
public class Garbage{
public static void main(String[] args){
if(args.length==0) {
System.out.println("Usage:n"+"java Garbage beforen or:n"+"java Garbage after");
return;
}
while(!Chair.f){
new Chair();
}
System.out.println("After all Chairs have been created:n"+"total created="+Chair.created+", total finalized=" +Chair.finalized);
if(args[0].equals("before")){
System.out.println("gc():");
System.gc();
System.out.println("runFinalization():");
System.runFinalization();
}
System.out.println("bye!");
if(args[0].equals("after"))
System.runFinalizersOnExit(true);
}
}
在while(!Chair.f){
new Chair();
...
}中,只要不符合条件,就会生成一个new Chair();而Chair构造器中只有一个累加和打印,系统为什么会自动停止循环,protected void finalize()什么时候开始运行?
class Chair{
static boolean gcrun=false;
static boolean f=false;
static int created=0;
static int finalized=0;
int i;
Chair(){
i=++created;
if(created==47)
System.out.println("Created 47");
}
protected void finalize(){
if(!gcrun){
gcrun=true;
System.out.println("Beginning to finalize after"+created +"Chairs have been created");
}
if(i==47){
System.out.println("Finalizing Chair #47,"+"Setting flag to stop Chair creation");
f=true;
}
finalized++;
if(finalized>=created)
System.out.println("All"+finalized+"finalized");
}
}
public class Garbage{
public static void main(String[] args){
if(args.length==0) {
System.out.println("Usage:n"+"java Garbage beforen or:n"+"java Garbage after");
return;
}
while(!Chair.f){
new Chair();
}
System.out.println("After all Chairs have been created:n"+"total created="+Chair.created+", total finalized=" +Chair.finalized);
if(args[0].equals("before")){
System.out.println("gc():");
System.gc();
System.out.println("runFinalization():");
System.runFinalization();
}
System.out.println("bye!");
if(args[0].equals("after"))
System.runFinalizersOnExit(true);
}
}
在while(!Chair.f){
new Chair();
...
}中,只要不符合条件,就会生成一个new Chair();而Chair构造器中只有一个累加和打印,系统为什么会自动停止循环,protected void finalize()什么时候开始运行?
|
程序改为:
class Chair{
static boolean gcrun=false;
static boolean f=false;
static long created=0;
static long finalized=0;
long i;
Chair(){
i=++created;
// System.out.println(created);
if(created==47)
System.out.println("Created 47");
}
protected void finalize(){
if(!gcrun){
gcrun=true;
System.out.println("Beginning to finalize after"+created +"Chairs have been created");
}
if(i==47){
System.out.println("Finalizing Chair #47,"+"Setting flag to stop Chair creation");
f=true;
}
finalized++;
if(finalized>=created)
{
System.out.println("All"+created+"created");
System.out.println("All"+finalized+"finalized");}
}
}
public class Garbage{
public static void main(String[] args){
if(args.length==0) {
System.out.println("Usage:n"+"java Garbage beforen or:n"+"java Garbage after");
return;
}
while(!Chair.f){
System.out.print("I LOVE:"+new Chair());
// ---------------------
}
System.out.println("After all Chairs have been created:n"+"total created="+Chair.created+", total finalized=" +Chair.finalized);
if(args[0].equals("before")){
System.out.println("gc():");
// System.gc();
System.out.println("runFinalization():");
System.runFinalization();
}
System.out.println("bye!");
if(args[0].equals("after"))
System.runFinalizersOnExit(true);
}
}
大家看一看这程序又是什么结果:
不就明白它的执行顺序???。
class Chair{
static boolean gcrun=false;
static boolean f=false;
static long created=0;
static long finalized=0;
long i;
Chair(){
i=++created;
// System.out.println(created);
if(created==47)
System.out.println("Created 47");
}
protected void finalize(){
if(!gcrun){
gcrun=true;
System.out.println("Beginning to finalize after"+created +"Chairs have been created");
}
if(i==47){
System.out.println("Finalizing Chair #47,"+"Setting flag to stop Chair creation");
f=true;
}
finalized++;
if(finalized>=created)
{
System.out.println("All"+created+"created");
System.out.println("All"+finalized+"finalized");}
}
}
public class Garbage{
public static void main(String[] args){
if(args.length==0) {
System.out.println("Usage:n"+"java Garbage beforen or:n"+"java Garbage after");
return;
}
while(!Chair.f){
System.out.print("I LOVE:"+new Chair());
// ---------------------
}
System.out.println("After all Chairs have been created:n"+"total created="+Chair.created+", total finalized=" +Chair.finalized);
if(args[0].equals("before")){
System.out.println("gc():");
// System.gc();
System.out.println("runFinalization():");
System.runFinalization();
}
System.out.println("bye!");
if(args[0].equals("after"))
System.runFinalizersOnExit(true);
}
}
大家看一看这程序又是什么结果:
不就明白它的执行顺序???。
|
那是因为,虽然生成的对象符合回收条件,但不一定会被马上回收。有可能是,都生成了几千个对象了,才有对象开始被回收。