博客
关于我
C# 对象池的实现(能限制最大实例数量,类似于WCF的MaxInstanceCount功能)
阅读量:737 次
发布时间:2019-03-22

本文共 3079 字,大约阅读时间需要 10 分钟。

对象池服务可以减少从头创建每个对象的系统开销。在激活对象时,它从池中提取。在停用对象时,它放回池中,等待下一个请求。

我们来看下主线程中,如何与对象池打交道:

static void Main(string[] args)        {            InstancePoolResolver.Register
(); 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
()) //从对象池中取得对象,没有可用对象则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
typeMappers = new Dictionary
(); private static Dictionary
typeMappersMaxInstanceCount = new Dictionary
(); private static Dictionary
> typeInstances = new Dictionary
>(); private static object o4lock = new object(); public static void Register
() 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
()); InstanceSettingAttribute setting = GetInstanceSettingAttribute(typeof(TProvider)); typeMappersMaxInstanceCount.Add(t, setting.MaxInstanceGlobal); } } public static T Resolve
() where T: PoolableObject { Type t = typeof(T); if (!typeMappers.ContainsKey(t) || !typeInstances.ContainsKey(t)) throw new Exception("Key empty, register please"); lock (o4lock) { List
instances = typeInstances[t]; if (instances == null) { instances = new List
(); 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的函数

 

转载地址:http://hdkwk.baihongyu.com/

你可能感兴趣的文章
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 添加列,修改列,删除列
查看>>
mysql 添加索引
查看>>
MySQL 添加索引,删除索引及其用法
查看>>
mysql 状态检查,备份,修复
查看>>
MySQL 用 limit 为什么会影响性能?
查看>>
MySQL 用 limit 为什么会影响性能?有什么优化方案?
查看>>
MySQL 用户权限管理:授权、撤销、密码更新和用户删除(图文解析)
查看>>
mysql 用户管理和权限设置
查看>>
MySQL 的 varchar 水真的太深了!
查看>>
mysql 的GROUP_CONCAT函数的使用(group_by 如何显示分组之前的数据)
查看>>
MySQL 的instr函数
查看>>
MySQL 的mysql_secure_installation安全脚本执行过程介绍
查看>>
MySQL 的Rename Table语句
查看>>
MySQL 的全局锁、表锁和行锁
查看>>