Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通用对象池_C#_Object Pooling - Fatal编程技术网

C# 通用对象池

C# 通用对象池,c#,object-pooling,C#,Object Pooling,是否可以创建一个通用对象池,在其中创建新对象? 另外,如果这个对象创建可以接收参数,那就太好了 public interface IPoolable { void Dispose(); } public class ObjectPool<T> where T : IPoolable { private List<T> pool; public T Get() {

是否可以创建一个通用对象池,在其中创建新对象? 另外,如果这个对象创建可以接收参数,那就太好了

    public interface IPoolable
    {
        void Dispose();
    }


    public class ObjectPool<T> where T : IPoolable
    {
        private List<T> pool;

        public T Get()
        {
            if(pool.count > 0)
            {
                return pool.Pop();
            }
            else
            {
                return new T(); //  <- How to do this properly?
            }
        }
    }

    public class SomeClass : IPoolable
    {
        int id;

        public SomeClass(int id)
        {
            this.id = id;
        }

        public void Dispose()
        {

        }
    }

    public class OtherClass : IPoolable
    {
        string name;
        int id;

        public OtherClass(string name, int id)
        {
            this.name = name;
            this.id = id;
        }

        public void Dispose()
        {

        }
    }
SomeClass a = myPool.Get(2);
OtherClass b = myOtherPool.Get("foo", 4);
或者,如果参数不可能,这也可以

SomeClass a = myPool.Get();
a.id = 2;
OtherClass b = myOtherPool.Get();
b.name = "foo";
b.id = 4;
你可以使用

像这样

return (T)Activator.CreateInstance(typeof(T), id);
但是,无法指定类型必须为构造函数提供参数;既不在接口声明中,也不在泛型类型约束中,也不在类继承中

像这样

return (T)Activator.CreateInstance(typeof(T), id);

但是,无法指定类型必须为构造函数提供参数;既不在接口声明中,也不在泛型类型约束中,也不在类继承中。

您可以这样做:

public class ObjectPool<T>
{
    private Queue<T> _pool = new Queue<T>();

    private const int _maxObjects = 100;  // Set this to whatever

    public T Get(params object[] parameters)
    {
        T obj;

        if (_pool.Count < 1)
            obj = (T)Activator.CreateInstance(typeof(T), parameters);
        else
            obj = _pool.Dequeue();

        return obj;
    }

    public void Put(T obj)
    {
        if (_pool.Count < _maxObjects)
            _pool.Enqueue(obj);
    }
}
公共类对象池
{
专用队列_pool=新队列();
private const int _maxObjects=100;//将其设置为任意值
公共T获取(参数对象[]参数)
{
T-obj;
如果(_pool.Count<1)
obj=(T)Activator.CreateInstance(typeof(T),参数);
其他的
obj=_pool.Dequeue();
返回obj;
}
公开作废认沽权证(T obj)
{
如果(_pool.Count<_maxObjects)
_排队(obj);
}
}

您可以这样做:

public class ObjectPool<T>
{
    private Queue<T> _pool = new Queue<T>();

    private const int _maxObjects = 100;  // Set this to whatever

    public T Get(params object[] parameters)
    {
        T obj;

        if (_pool.Count < 1)
            obj = (T)Activator.CreateInstance(typeof(T), parameters);
        else
            obj = _pool.Dequeue();

        return obj;
    }

    public void Put(T obj)
    {
        if (_pool.Count < _maxObjects)
            _pool.Enqueue(obj);
    }
}
公共类对象池
{
专用队列_pool=新队列();
private const int _maxObjects=100;//将其设置为任意值
公共T获取(参数对象[]参数)
{
T-obj;
如果(_pool.Count<1)
obj=(T)Activator.CreateInstance(typeof(T),参数);
其他的
obj=_pool.Dequeue();
返回obj;
}
公开作废认沽权证(T obj)
{
如果(_pool.Count<_maxObjects)
_排队(obj);
}
}

我在寻找类似的东西,遇到了以下问题:

public class ObjectPool<TObject>
    {
        private int maxPoolSize;
        private SpinLock poolLock;
        private Dictionary<Type, Stack<TObject>> poolCache;
        private Func<TObject> factory;

        public ObjectPool(int poolSize)
        {
            this.maxPoolSize = poolSize;
            this.poolLock = new SpinLock(false);
            this.poolCache = new Dictionary<Type, Stack<TObject>>();
        }

        public ObjectPool(int poolSize, Func<TObject> factory) : this(poolSize)
        {
            this.factory = factory;
        }

        public T Rent<T>() where T : TObject
            => (T)this.Rent(typeof(T));

        public TObject Rent(Type type)
        {
            bool lockTaken = false;
            Stack<TObject> cachedCollection;
            this.poolLock.Enter(ref lockTaken);

            try
            {
                if (!this.poolCache.TryGetValue(type, out cachedCollection))
                {
                    cachedCollection = new Stack<TObject>();
                    this.poolCache.Add(type, cachedCollection);
                }
            }
            finally
            {
                if (lockTaken)
                {
                    this.poolLock.Exit(false);
                }
            }

            if (cachedCollection.Count > 0)
            {
                TObject instance = cachedCollection.Pop();
                if (instance != null)
                    return instance;
            }

            // New instances don't need to be prepared for re-use, so we just return it.
            if (this.factory == null)
            {
                return (TObject)Activator.CreateInstance(type);
            }
            else
            {
                return this.factory();
            }
        }

        public void Return(TObject instanceObject)
        {
            Stack<TObject> cachedCollection = null;
            Type type = typeof(TObject);

            bool lockTaken = false;
            this.poolLock.Enter(ref lockTaken);
            try
            {
                if (!this.poolCache.TryGetValue(type, out cachedCollection))
                {
                    cachedCollection = new Stack<TObject>();
                    this.poolCache.Add(type, cachedCollection);
                }

                if (cachedCollection.Count >= this.maxPoolSize)
                {
                    return;
                }

                cachedCollection.Push(instanceObject);
            }
            finally
            {
                if (lockTaken)
                {
                    this.poolLock.Exit(false);
                }
            }
        }
    }
公共类对象池
{
私有int-maxPoolSize;
私有自旋锁池锁;
专用字典池缓存;
私营Func工厂;
公共对象池(int poolSize)
{
this.maxPoolSize=池大小;
this.poolLock=新自旋锁(false);
this.poolCache=新字典();
}
公共对象池(int poolSize,Func工厂):此(poolSize)
{
这个工厂=工厂;
}
公共T租金()其中T:ToObject
=>(T)该租金(类型(T));
公共托宾租金(类型)
{
bool-locktake=false;
堆栈缓存集合;
this.poolLock.Enter(ref locktake);
尝试
{
if(!this.poolCache.TryGetValue(类型,out cachedCollection))
{
cachedCollection=新堆栈();
this.poolCache.Add(类型,cachedCollection);
}
}
最后
{
如果(已锁定)
{
this.poolLock.Exit(false);
}
}
如果(cachedCollection.Count>0)
{
ToObject实例=cachedCollection.Pop();
if(实例!=null)
返回实例;
}
//新实例不需要为重用做好准备,所以我们只返回它。
if(this.factory==null)
{
return(TObject)Activator.CreateInstance(type);
}
其他的
{
返回此.factory();
}
}
公共无效返回(ToObject instanceObject)
{
Stack cachedCollection=null;
类型=类型的类型(TObject);
bool-locktake=false;
this.poolLock.Enter(ref locktake);
尝试
{
if(!this.poolCache.TryGetValue(类型,out cachedCollection))
{
cachedCollection=新堆栈();
this.poolCache.Add(类型,cachedCollection);
}
如果(cachedCollection.Count>=此.maxPoolSize)
{
回来
}
cachedCollection.Push(instanceObject);
}
最后
{
如果(已锁定)
{
this.poolLock.Exit(false);
}
}
}
}
这是一个非常好的实现,我无耻地从中偷了


它支持对从泛型类型参数继承的任何对象进行池化。

我正在寻找类似的对象,并遇到以下问题:

public class ObjectPool<TObject>
    {
        private int maxPoolSize;
        private SpinLock poolLock;
        private Dictionary<Type, Stack<TObject>> poolCache;
        private Func<TObject> factory;

        public ObjectPool(int poolSize)
        {
            this.maxPoolSize = poolSize;
            this.poolLock = new SpinLock(false);
            this.poolCache = new Dictionary<Type, Stack<TObject>>();
        }

        public ObjectPool(int poolSize, Func<TObject> factory) : this(poolSize)
        {
            this.factory = factory;
        }

        public T Rent<T>() where T : TObject
            => (T)this.Rent(typeof(T));

        public TObject Rent(Type type)
        {
            bool lockTaken = false;
            Stack<TObject> cachedCollection;
            this.poolLock.Enter(ref lockTaken);

            try
            {
                if (!this.poolCache.TryGetValue(type, out cachedCollection))
                {
                    cachedCollection = new Stack<TObject>();
                    this.poolCache.Add(type, cachedCollection);
                }
            }
            finally
            {
                if (lockTaken)
                {
                    this.poolLock.Exit(false);
                }
            }

            if (cachedCollection.Count > 0)
            {
                TObject instance = cachedCollection.Pop();
                if (instance != null)
                    return instance;
            }

            // New instances don't need to be prepared for re-use, so we just return it.
            if (this.factory == null)
            {
                return (TObject)Activator.CreateInstance(type);
            }
            else
            {
                return this.factory();
            }
        }

        public void Return(TObject instanceObject)
        {
            Stack<TObject> cachedCollection = null;
            Type type = typeof(TObject);

            bool lockTaken = false;
            this.poolLock.Enter(ref lockTaken);
            try
            {
                if (!this.poolCache.TryGetValue(type, out cachedCollection))
                {
                    cachedCollection = new Stack<TObject>();
                    this.poolCache.Add(type, cachedCollection);
                }

                if (cachedCollection.Count >= this.maxPoolSize)
                {
                    return;
                }

                cachedCollection.Push(instanceObject);
            }
            finally
            {
                if (lockTaken)
                {
                    this.poolLock.Exit(false);
                }
            }
        }
    }
公共类对象池
{
私有int-maxPoolSize;
私有自旋锁池锁;
专用字典池缓存;
私营Func工厂;
公共对象池(int poolSize)
{
this.maxPoolSize=池大小;
this.poolLock=新自旋锁(false);
this.poolCache=新字典();
}
公共对象池(int poolSize,Func工厂):此(poolSize)
{
这个工厂=工厂;
}
公共T租金()其中T:ToObject
=>(T)该租金(类型(T));
公共托宾租金(类型)
{
bool-locktake=false;
堆栈缓存集合;
this.poolLock.Enter(ref locktake);
尝试
{
if(!this.poolCache.TryGetValue(类型,out cachedCollection))
{
cachedCollection=新堆栈();
this.poolCache.Add(类型,cachedCollection);
}
}
最后
{
如果(已锁定)
{
this.poolLock.Exit(false);
}
}
如果(cachedCollection.Count>0)
{
ToObject实例=cachedCollection.Pop();
if(实例!=nul