class expensive{
static stack pool = new stack();
public static expensive getobjectfrompool(){
retun (expensive) pool.pop();
}
public static void shutdownthepool(){
pool = null;
}
public expensive(){
//先构造对象
pool.push(this);
}
finalize (){
if(pool!=null){
gc.registerforfinally(this;)//先把他叫醒挨宰
pool.push(this);//将“清醒的”对象加入到对象池中,让他起死回生
}
}
}
class app{
static void main(){
new expensive();
……
expensive e = expensiv. getobjectfrompool();
//下面就可以使用e了
expensive.shutdownthepool();//关闭应用程序前,先关闭对象池,否则会在内存中留下“孔洞”,因为finalize已经被重写了
}
}
新闻热点
疑难解答
图片精选