在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
其实这是我以前用vs2003写的代码,现在修改为c#2.0 不过对一些简单的对象还是建议不要用object pool,用了效果还比不上new.
class ObjectPool<T>
{ PoolItem<T> m_Cur; public ObjectPool() { } public T GetNew() { lock (this) { if (null == m_Cur) { return System.Activator.CreateInstance<T>(); } else { T ret = m_Cur.Next.PoolObject; m_Cur = m_Cur.Next; return ret; } } } public void Free(T o) { lock (this) { if (null == m_Cur) { m_Cur = new PoolItem<T>(o); } else { m_Cur.Next = new PoolItem<T>(o); } } } class PoolItem<TT> { public PoolItem(TT o) { PoolObject = o; } public TT PoolObject; public PoolItem<TT> Next; } |
请发表评论