基于泛型的,高性能的,可指定构造函数及传入参数初始化的,线程安全的,扩展性非常高的 传说中的 对象池
2010-05-10 22:15
453 查看
基于泛型的,高性能的,可指定构造函数及传入参数初始化的,线程安全的,扩展性非常高的 传说中的 对象池
测试代码:
关键代码:
代码下载地址:
测试代码:
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); } }
代码下载地址:
相关文章推荐
- 数组属性,根据参数动态设置类中数组的长度!构造函数传入数组长度!并初始化数组!
- 初始化与清理(构造函数初始化,可变参数列表,enum)
- 在只需要一个指定正确的参数的情况下如何防止传入其他干扰的参数
- --将传入的一个参数按指定分隔符切分到一个表中
- 为指定初始化参数
- DELPHI基于线程的定时器和一个泛型对象池
- 第四周项目一——三角形类的构造函数——参数初始化表
- ora-02095无法修改指定的初始化参数
- 第五周项目1.4在构造函数中使用参数初始化表对数据成员初始化
- DataTable数据转换为List和泛型对构造函数带参数的类创建实例的方法
- 第3周-项目1-三角形类的构造函数-使用参数初始化表对数据成员初始化
- 第4周项目1-三角形类的构造函数(4)-使用参数初始化表
- js函数:改变页面url替换指定传入参数的值
- 关于模板template的泛型参数的构造函数
- c#实验笔记- params关键字可以指定可变数目的变量 for循环可以初始化多个参数
- [置顶] 第五周项目一构造函数————4)参数初始化表
- C#的泛型的类型参数可以有带参数的构造函数的约束方式吗?
- Silverlight 如何:指定和检索自定义初始化参数
- 黑马程序员——通过反射获取方法中泛型参数的指定类型
- 构造函数及其参数列表初始化问题