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已經被重寫了
}
}
新聞熱點
疑難解答
圖片精選