コード例 #1
0
        /// <summary>
        /// 获取一个item
        /// </summary>
        /// <returns></returns>
        public T GetItem()
        {
            ObjectPoolContainer <T> container = null;

            for (int i = 0; i < list.Count; i++)
            {
                lastIndex++;
                if (lastIndex > list.Count - 1)
                {
                    lastIndex = 0;
                }

                if (list[lastIndex].Used)
                {
                    continue;
                }
                else
                {
                    container = list[lastIndex];
                    break;
                }
            }

            //没有合适的则创建一个新的
            if (container == null)
            {
                container = CreateContainer();
            }
            container.Consume();
            lookup.Add(container.Item, container);
            return(container.Item);
        }
コード例 #2
0
        private ObjectPoolContainer <T> CreateContainer()
        {
            var container = new ObjectPoolContainer <T>();

            container.Item = factoryFunc();
            list.Add(container);
            return(container);
        }