手痒痒,也来个c# 2.0 object pool

看到 http://www.cnblogs.com/teddyma/archive/2006/01/11/315158.html写了个object pool,心痒痒的,也跟着写了个.
其实这是我以前用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;
        }

你可能感兴趣的:(object)