using System.Collections.Concurrent; namespace QFSW.QC.Pooling { public class ConcurrentPool : IPool where T : class, new() { private readonly ConcurrentBag _objs; public ConcurrentPool() { _objs = new ConcurrentBag(); } public ConcurrentPool(int objCount) { _objs = new ConcurrentBag(); for (int i = 0; i < objCount; i++) { _objs.Add(new T()); } } public T GetObject() { if (_objs.TryTake(out T obj)) { return obj; } return new T(); } public void Release(T obj) { _objs.Add(obj); } } }