C# 对象池的实现(能限制最大实例数量,类似于WCF的MaxInstanceCount功能)

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的函数

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值