日期:2010-08-01 浏览次数:20526 次
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已经被重写了
}
}