基于泛型的,高性能的,可指定构造函数及传入参数初始化的,线程安全的,扩展性非常高的 传说中的 对象池 测试代码: static void Main(string[] args) { ObjectPool<SqlConnection> ConnPool = new ObjectPool<SqlConnection>(100); ConnPool.GetObjectRunTime = new ObjectPool<SqlConnection>.ObjectRunTimeHandle((p, x) => { p.Open(); return p; }); ConnPool.ReleaseObjectRunTime = new ObjectPool<SqlConnection>.ObjectRunTimeHandle((p, x) => { p.Close(); return p; }); ConnPool.TheConstructor = typeof(SqlConnection).GetConstructor(new Type[]{typeof(string)}); ConnPool.param = new object[] { @"Data Source=LUYIKK-PC/SQLEXPRESS;Initial Catalog=priceindex;Integrated Security=True; Min Pool Size=20;" }; //SqlConnection[] xm = ConnPool.GetObject(100); //ConnPool.ReleaseObject(xm); Task Task1 = new Task(() => { Parallel.For(0, 100, (i) => { SqlConnection p = ConnPool.GetObject(); try { using (SqlCommand command = new SqlCommand("SELECT count(ID) as cout FROM [priceindex].[dbo].[PriceIndex_Prices]", p)) { SqlDataReader read = command.ExecuteReader(); while (read.Read()) { Console.WriteLine("{0} {1}--- 1 " + read[0].ToString(), ConnPool.ObjectStack.Count, i); } read.Close(); read.Dispose(); } } catch { throw; } finally { ConnPool.ReleaseObject(p); } }); }); Task Task2 = new Task(() => { Parallel.For(0, 100, (i) => { SqlConnection p = ConnPool.GetObject(); try { using (SqlCommand command = new SqlCommand("SELECT count(ID) as cout FROM [priceindex].[dbo].[PriceIndex_Prices]", p)) { SqlDataReader read = command.ExecuteReader(); while (read.Read()) { Console.WriteLine("{0} {1}--- 2 " + read[0].ToString(), ConnPool.ObjectStack.Count, i); } read.Close(); read.Dispose(); } } catch { throw; } finally { ConnPool.ReleaseObject(p); } }); }); Task Task3 = new Task(() => { Parallel.For(0, 100, (i) => { SqlConnection p = ConnPool.GetObject(); try { using (SqlCommand command = new SqlCommand("SELECT count(ID) as cout FROM [priceindex].[dbo].[PriceIndex_Prices]", p)) { SqlDataReader read = command.ExecuteReader(); while (read.Read()) { Console.WriteLine("{0} {1}--- 3 " + read[0].ToString(), ConnPool.ObjectStack.Count, i); } read.Close(); read.Dispose(); } } catch { throw; } finally { ConnPool.ReleaseObject(p); } }); }); System.Diagnostics.Stopwatch stp = new System.Diagnostics.Stopwatch(); stp.Start(); Task3.Start(); Task2.Start(); Task1.Start(); Task3.Wait(); Task2.Wait(); Task1.Wait(); stp.Stop(); Console.WriteLine("..........ObjectStack.Count {0} .....stp.ElapsedMilliseconds {1}", ConnPool.ObjectStack.Count, stp.ElapsedMilliseconds); Console.ReadLine(); 关键代码: /// <summary> /// 获取对象 /// </summary> /// <returns></returns> public T GetObject() { if (ObjectStack.Count == 0) { T p = GetT(); if (GetObjectRunTime != null) p = GetObjectRunTime(p, this); return p; } else { T p; if (ObjectStack.TryPop(out p)) { if (GetObjectRunTime != null) p = GetObjectRunTime(p, this); return p; } else { p= GetT(); if (GetObjectRunTime != null) p = GetObjectRunTime(p, this); return p; } } } /// <summary> /// 回收对象 /// </summary> /// <param name="obj"></param> public void ReleaseObject(T obj) { if (ReleaseObjectRunTime != null) { obj = ReleaseObjectRunTime(obj, this); } if (ObjectStack.Count >= MaxObjectCount) { if (obj is IDisposable) { ((IDisposable)obj).Dispose(); } } else { ObjectStack.Push(obj); } } 代码下载地址: