以文本方式查看主题 - 中文XML论坛 - 专业的XML技术讨论区 (http://bbs.xml.org.cn/index.asp) -- 『 Dot NET,C#,ASP,VB 』 (http://bbs.xml.org.cn/list.asp?boardid=43) ---- 万能对象池【C#实现】 (http://bbs.xml.org.cn/dispbbs.asp?boardid=43&rootid=&id=76599) |
-- 作者:卷积内核 -- 发布时间:9/5/2009 8:58:00 AM -- 万能对象池【C#实现】 如果一种类型的对象需要经常被创建、销毁,为了提高性能,我们通常需要使用“池”技术,就如线程池、TCP连接池等一样。那么需要使用池技术的对象一般有哪些特征了? (1)创建过程耗时 (2)不需要保存客户状态 (3)对象体积较大 (4)频繁创建/销毁 为了省事,我希望实现一个万能对象池组件,该对象池可以缓存任意类型的对象。下面给出对象池的接口: public interface IObjectPool int MinObjCount {get ;} event CallBackObjPool PoolShrinked ; public delegate void CallBackObjPool() ; 我们可以考虑这样一种情况,当我们需要缓存的对象需要维持和一个客户之间的状态,那么也是可以的,如果是这样,所缓存的类型最好实现下面的IPooledObjSupporter接口。 public interface IPooledObjSupporter : IDisposable using System; namespace EnterpriseServerBase.Infrastructure IObjectPool 成员#region IObjectPool 成员 public bool Initialize(Type objType, object[] cArgs, int minNum, int maxNum) this.destType = objType ; //缓存的类型是否支持IPooledObjSupporter接口 this.InstanceObjects() ; return true ; private void InstanceObjects() CreateOneObject ,DistroyOneObject#region CreateOneObject ,DistroyOneObject try if(this.MemoryUseOut != null) return -1 ; int key = obj.GetHashCode() ; return key ; private void DistroyOneObject(int key) this.hashTableObjs.Remove(key) ; public object RentObject() if(target == null) return target ; GiveBackObject#region GiveBackObject lock(this) if(this.CanShrink()) //能够收缩对象池 return (busyCount < this.shrinkPoint) && (this.CurObjCount > (this.minObjCount + (this.maxObjCount - this.minObjCount)/2)) ; if(destKey != -1) if(this.PoolShrinked != null) public void Dispose() this.hashTableStatus.Clear() ; property#region property public int MaxObjCount public int CurObjCount public int IdleObjCount private int GetIdleObjCount() return count ; #endregion |
-- 作者:卷积内核 -- 发布时间:9/5/2009 8:58:00 AM -- 对象在实现该接口后,就可以被对象池在收到归还的对象时重置其状态了。整个对象池的实现代码如下: using System; namespace EnterpriseServerBase.Infrastructure IObjectPool 成员#region IObjectPool 成员 public bool Initialize(Type objType, object[] cArgs, int minNum, int maxNum) this.destType = objType ; //缓存的类型是否支持IPooledObjSupporter接口 this.InstanceObjects() ; return true ; private void InstanceObjects() CreateOneObject ,DistroyOneObject#region CreateOneObject ,DistroyOneObject try if(this.MemoryUseOut != null) return -1 ; int key = obj.GetHashCode() ; return key ; private void DistroyOneObject(int key) this.hashTableObjs.Remove(key) ; public object RentObject() if(target == null) return target ; GiveBackObject#region GiveBackObject lock(this) if(this.CanShrink()) //能够收缩对象池 return (busyCount < this.shrinkPoint) && (this.CurObjCount > (this.minObjCount + (this.maxObjCount - this.minObjCount)/2)) ; if(destKey != -1) if(this.PoolShrinked != null) public void Dispose() this.hashTableStatus.Clear() ; property#region property public int MaxObjCount public int CurObjCount public int IdleObjCount private int GetIdleObjCount() return count ; #endregion |
W 3 C h i n a ( since 2003 ) 旗 下 站 点 苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》 |
156.250ms |