Skip to content

Instantly share code, notes, and snippets.

@rdeioris
Created May 9, 2017 18:41
Show Gist options
  • Save rdeioris/e27ddd87616147445a640b73e6d2c179 to your computer and use it in GitHub Desktop.
Save rdeioris/e27ddd87616147445a640b73e6d2c179 to your computer and use it in GitHub Desktop.
public static class PoolManager
{
private const int initialItems = 10;
private static Dictionary<Type, Queue<object>> pools;
public static T Get<T>() where T : class, new()
{
if (!pools.ContainsKey(typeof(T)))
{
pools[typeof(T)] = new Queue<T>() as Queue<object>;
for (int i = 0; i < initialItems; i++)
{
pools[typeof(T)].Enqueue(new T());
}
}
return pools[typeof(T)].Dequeue() as T;
}
public static void ReEnqueue(object item)
{
pools[item.GetType()].Enqueue(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment