using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ObjectPool { class MyConnectionPool { private readonly ConcurrentBag pool = new ConcurrentBag(); public MyConnection GetObject() { MyConnection obj; if (pool.TryTake(out obj)) return obj; else return new MyConnection(); } public void ReleaseObject(MyConnection conn) { pool.Add(conn); Console.WriteLine($"Release: {conn.GetHashCode()}"); } } }