예제 #1
0
        public Int32 DecreaseSize(Int32 size)
        {
            Int32 nDecrease = size;

            lock (this)
            {
                if (nDecrease <= 0)
                {
                    return(0);
                }
                if (nDecrease > _listFreeIndex.Count)
                {
                    nDecrease = _listFreeIndex.Count;
                }

                for (int i = 0; i < nDecrease; i++)
                {
                    _listObjects.Remove(_listFreeIndex[i]);
                }

                _listFreeIndex.Clear();
                _listUsingIndex.Clear();

                foreach (DictionaryEntry de in _listObjects)
                {
                    PoolItem pitem = (PoolItem)de.Value;
                    if (pitem.Using)
                    {
                        _listUsingIndex.Add(pitem.InnerObjectHashcode);
                    }
                    else
                    {
                        _listFreeIndex.Add(pitem.InnerObjectHashcode);
                    }
                }
            }
            _nCurrentSize -= nDecrease;
            return(nDecrease);
        }
예제 #2
0
        public ObjectPool(Type type, Object create_param, Int32 init_size, Int32 capacity)
        {
            if (init_size < 0 || capacity < 1 || init_size > capacity)
            {
                throw (new Exception("Invalid parameter!"));
            }

            _nCapacity      = capacity;
            _listObjects    = new Hashtable(capacity);
            _listFreeIndex  = new ArrayList(capacity);
            _listUsingIndex = new ArrayList(capacity);
            _typeObject     = type;
            _objCreateParam = create_param;

            for (int i = 0; i < init_size; i++)
            {
                PoolItem pitem = new PoolItem(type, create_param);
                _listObjects.Add(pitem.InnerObjectHashcode, pitem);
                _listFreeIndex.Add(pitem.InnerObjectHashcode);
            }

            _nCurrentSize = _listObjects.Count;
        }