程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C# 對象池的實現(能限制最大實例數量,類似於WCF的MaxInstanceCount功能)

C# 對象池的實現(能限制最大實例數量,類似於WCF的MaxInstanceCount功能)

編輯:關於C#

對象池服務可以減少從頭創建每個對象的系統開銷。在激活對象時,它從池中提取。在停用對象時 ,它放回池中,等待下一個請求。

我們來看下主線程中,如何與對象池打交道:

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

查看本欄目

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved