Ejemplo n.º 1
0
        /// <summary>
        /// 获取一个缓存对象,当缓存用尽时,将根据OperateAtExhaust处理
        /// </summary>
        /// <returns></returns>
        public T GetInstance()
        {
            T Result = null;

            #region 获取对象

            EventSync.WaitOne();

            if (IdleCount < 0)
            {
                if (OperateAtExhaust.Equals(EnumOperateAtExhaust.CreateNew))
                {
                    Result = new T();
                }
            }
            else
            {
                if (object.ReferenceEquals(null, Cache[--IdleCount]))
                {
                    Cache[IdleCount] = new T();
                }
                Result = Cache[IdleCount];
            }

            #endregion

            if (object.ReferenceEquals(null, Result) && OperateAtExhaust.Equals(EnumOperateAtExhaust.Suspend))
            {
                //缓存用尽时阻塞线程,等待其它线程释放对象
                WaitHandle.SignalAndWait(EventSync, EventObjRelease);
                return(GetInstance());
            }
            else
            {
                EventSync.Set();
                return(Result);
            }

            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取一个缓存对象,当缓存用尽时,将根据OperateAtExhaust处理
        /// </summary>
        /// <returns></returns>
        public T GetInstance()
        {
            T Result = null;

            if (IdleCount < 0)
            {
                if (OperateAtExhaust.Equals(EnumOperateAtExhaust.CreateNew))
                {
                    Result = new T();
                }
            }
            else
            {
                if (object.ReferenceEquals(null, Cache[--IdleCount]))
                {
                    Cache[IdleCount] = new T();
                }
                Result = Cache[IdleCount];
            }

            return(Result);
        }