C# 对象池的实现(能限制最大实例数量,类似于WCF的MaxInstanceCount功能)
来源:互联网 发布:西安软件学校排名 编辑:程序博客网 时间:2024/05/17 22:27
http://www.cnblogs.com/aarond/p/ObjectPool.html
对象池服务可以减少从头创建每个对象的系统开销。在激活对象时,它从池中提取。在停用对象时,它放回池中,等待下一个请求。
我们来看下主线程中,如何与对象池打交道:
static void Main(string[] args) { InstancePoolResolver.Register<OrderQueryServiceInterface, OrderQueryService>(); while (true) { Thread.Sleep(2000); Console.Clear(); for (int i = 0; i < 20;i++ ) { ThreadPool.QueueUserWorkItem(new WaitCallback(ConsumeObject)); } } } private static void ConsumeObject(object state) { OrderQueryServiceInterface srv = null; try { using (srv = InstancePoolResolver.Resolve<OrderQueryServiceInterface>()) //从对象池中取得对象,没有可用对象则throw exception { Console.WriteLine("Object ID--->" + srv.GetHashCode()); Thread.Sleep(1000); //故意长时间占用对象 } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { if (srv != null) srv.Dispose(); } }
运行效果:
最多只有2个instance,那在哪里设置这个数量呢?请看
是通过Attribute打tag上去的
下面来看看最核心的InstancePoolResolver类
public sealed class InstancePoolResolver { private static Dictionary<Type, Type> typeMappers = new Dictionary<Type, Type>(); private static Dictionary<Type, int> typeMappersMaxInstanceCount = new Dictionary<Type, int>(); private static Dictionary<Type, List<PoolableObject>> typeInstances = new Dictionary<Type, List<PoolableObject>>(); private static object o4lock = new object(); public static void Register<T, TProvider>() where TProvider : class, new() { if (typeMappers.ContainsKey(typeof(T))) throw new Exception("Key existed"); lock (o4lock) { Type t = typeof(T); typeMappers.Add(t, typeof(TProvider)); typeInstances.Add(t, new List<PoolableObject>()); InstanceSettingAttribute setting = GetInstanceSettingAttribute(typeof(TProvider)); typeMappersMaxInstanceCount.Add(t, setting.MaxInstanceGlobal); } } public static T Resolve<T>() where T: PoolableObject { Type t = typeof(T); if (!typeMappers.ContainsKey(t) || !typeInstances.ContainsKey(t)) throw new Exception("Key empty, register please"); lock (o4lock) { List<PoolableObject> instances = typeInstances[t]; if (instances == null) { instances = new List<PoolableObject>(); typeInstances[t] = instances; } foreach (PoolableObject o in instances)//是否已经存在已有闲置对象 { if (o.IsInPool) { o.IsInPool = false; return (T)o; } } if (instances.Count < typeMappersMaxInstanceCount[t])//new新对象到对象池中 { Type type = typeMappers[t]; PoolableObject obj = (PoolableObject)Activator.CreateInstance(type); instances.Add(obj); obj.IsInPool = false; return (T)obj; } } throw new Exception("Object Pool fulled!"); //没有多余的资源 } private static InstanceSettingAttribute GetInstanceSettingAttribute(Type type) { object[] attrs = type.GetCustomAttributes(typeof(InstanceSettingAttribute), false); if (attrs == null || attrs.Count() == 0) return new InstanceSettingAttribute() { MaxInstanceGlobal=10}; return (InstanceSettingAttribute)attrs[0]; } }
其实很简单,只是多了个获取Attribute的函数
- C# 对象池的实现(能限制最大实例数量,类似于WCF的MaxInstanceCount功能)
- C# 对象池的实现(能限制最大实例数量,类似于WCF的MaxInstanceCount功能)
- 限制某个类所能产生的对象数量
- 悟: c实现类似于c++面向对象的功能
- Python 限制线程的最大数量(Semaphore)
- 条款二十六:限制某个class所能产生的对象的数量
- 条款26:限制某个class所能产生的对象数量
- 技术(2)—限制某个class所能产生的对象数量
- 10.限制某个类所能产生的对象数量——之一
- 11.限制某个类所能产生的对象数量——之二
- 《More Effective C++》条款26:限制某个Class所能产生的对象数量
- More Effective C++----(26)限制某个类所能产生的对象数量
- effective C++——限制类所能产生的对象数量
- c++限制某个class所能产生对象数量的方法
- 实现类似于购物车控制商品数量的按钮
- 实现类似于新浪微博@的功能
- 实现类似于新浪微博的@功能
- WCF服务中连接池中的最大连接数量的设置
- Junit使用教程(四)
- ORA-00054 锁表问题
- CRC_16 原理及其C语言算法实现
- 设计模式C++描述----17.备忘录(Memento)模式
- 父页面与子页面之间相互操作
- C# 对象池的实现(能限制最大实例数量,类似于WCF的MaxInstanceCount功能)
- java程序运行脚本
- MyCCL特征码定位原理学习
- 使用adb命令批处理安装&卸载apk
- SAS宏技术中,%let和call symput有什么区别?
- CRC_8 算法实现 C语言
- POJ 2427 pell方程
- NAS
- 学习笔记